stream-feed-flutter
stream feed official flutter sdk. build your own feed experience using dart and flutter.
�� quick links
- register to get an api key for stream activity feeds
- stream activity feeds ui kit
�� installation
install from pub
next step is to add stream_feed
to your dependencies, to do that just open pubspec.yaml and add it inside the dependencies section.
dependencies:
flutter:
sdk: flutter
stream_feed: ^0.0.1
using with flutter
this package can be integrated into flutter applications. remember to not expose the app secret in your flutter web apps, mobile apps, or other non-trusted environments like desktop apps.
�� usage
api client setup serverside + clientside
if you want to use the api client directly on your web/mobile app you need to generate a user token server-side and pass it.
server-side token generation
// instantiate a new client (server side)
const apikey = 'my-api-key';
const secret = 'my-api-secret';
// instantiate a new client (server side)
var client = streamclient.connect(apikey, secret: secret);
// optionally supply the app identifier and an options object specifying the data center to use and timeout for requests (15s)
client = streamclient.connect(apikey,
secret: secret,
appid: 'yourappid',
options: streamhttpclientoptions(
location: location.useast, connecttimeout: duration(seconds: 15)));
// create a token for user with id "the-user-id"
final usertoken = client.frontendtoken('the-user-id');
:warning: for security, you must never expose your api secret or generated client side token, and it’s highly recommended to use
exp
claim in client side token.
client api init
// instantiate new client with a user token
var client = streamclient.connect(apikey, token: token('usertoken'));
�� examples
// instantiate a feed object server side
var user1 = client.flatfeed('user', '1');
// get activities from 5 to 10 (slow pagination)
final activities = await user1.getactivities(limit: 5, offset: 5);
// filter on an id less than a given uuid
final filtered_activities = await user1.getactivities(
limit: 5,
filter: filter().idlessthan('e561de8f-00f1-11e4-b400-0cc47a024be0')
// all api calls are performed asynchronous and return a promise object
await user1
.getactivities(
limit: 5,
filter: filter().idlessthan('e561de8f-00f1-11e4-b400-0cc47a024be0'))
.then((value) => /* on success */
print(value))
.onerror((error,
stacktrace) => /* on failure, reason.error contains an explanation */
print(error));
// create a new activity
final activity = activity( actor: '1', verb: 'tweet', object: '1', foreignid: 'tweet:1' );
final added_activity = await user1.addactivity(activity);
// create a bit more complex activity
final complex_activity = activity(
actor: '1',
verb: 'run',
object: '1',
foreignid: 'run:1',
extradata: {
'course': {'name': 'golden gate park', 'distance': 10},
'participants': ['thierry', 'tommaso'],
'started_at': datetime.now().toiso8601string(),
},
);
final added_complex_activity = await user1.addactivity(complex_activity);
// remove an activity by its id
await user1.removeactivitybyid('e561de8f-00f1-11e4-b400-0cc47a024be0');
// or remove by the foreign id
await user1.removeactivitybyforeignid('tweet:1');
// mark a notification feed as read
await notification1.getactivities(
marker: activitymarker().allread(),
);
// mark a notification feed as seen
await notification1.getactivities(
marker: activitymarker().allseen(),
);
// follow another feed
await user1.follow(client.flatfeed('flat', '42'));
// stop following another feed
await user1.unfollow(client.flatfeed('flat', '42'));
// stop following another feed while keeping previously published activities
// from that feed
await user1.unfollow(client.flatfeed('flat', '42'), keephistory: true);
// follow another feed without copying the history
await user1.follow(client.flatfeed('flat', '42'), activitycopylimit: 0);
// list followers, following
await user1.getfollowers(limit: 10, offset: 10);
await user1.getfollowed(limit: 10, offset: 0);
await user1.follow(client.flatfeed('flat', '42'));
// adding multiple activities
const activities = [
activity(actor: '1', verb: 'tweet', object: '1'),
activity(actor: '2', verb: 'tweet', object: '3'),
];
await user1.addactivities(activities);
// specifying additional feeds to push the activity to using the to param
// especially useful for notification style feeds
final to = feedid.fromids(['user:2', 'user:3']);
final activityto = activity(
to: to,
actor: '1',
verb: 'tweet',
object: '1',
foreignid: 'tweet:1',
);
await user1.addactivity(activityto);
// adding one activity to multiple feeds
final feeds = feedid.fromids(['flat:1', 'flat:2', 'flat:3', 'flat:4']);
final activitytarget = activity(
actor: 'user:2',
verb: 'pin',
object: 'place:42',
target: 'board:1',
);
// ⚠️ server-side only!
await client.batch.addtomany(activitytarget, feeds!);
// batch create follow relations (let flat:1 follow user:1, user:2 and user:3 feeds in one single request)
const follows = [
follow('flat:1', 'user:1'),
follow('flat:1', 'user:2'),
follow('flat:1', 'user:3'),
];
// ⚠️ server-side only!
await client.batch.followmany(follows);
// updating parts of an activity
final set = {
'product.price': 19.99,
shares: {
facebook: '...',
twitter: '...',
},
};
final unset = ['daily_likes', 'popularity'];
// ...by id
final update = activityupdate.withid( '54a60c1e-4ee3-494b-a1e3-50c06acb5ed4', set, unset);
await client.updateactivitybyid(update);
// ...or by combination of foreign id and time
const timestamp = datetime.now();
const foreignid= 'product:123';
final update2 = activityupdate.withforeignid(
foreignid,
timestamp,
set,
unset,
);
await client.updateactivitybyid(update2);
// update the 'to' fields on an existing activity
// client.flatfeed("user", "ken").function (foreign_id, timestamp, new_targets, added_targets, removed_targets)
// new_targets, added_targets, and removed_targets are all arrays of feed ids
// either provide only the `new_targets` parameter (will replace all targets on the activity),
// or provide the added_targets and removed_targets parameters
// note - the updateactivitytotargets method is not intended to be used in a browser environment.
await client.flatfeed('user', 'ken').updateactivitytotargets('foreign_id:1234', timestamp, ['feed:1234']);
await client.flatfeed('user', 'ken').updateactivitytotargets('foreign_id:1234', timestamp, null, ['feed:1234']);
await client.flatfeed('user', 'ken').updateactivitytotargets('foreign_id:1234', timestamp, null, null, ['feed:1234']);
realtime (faye)
stream uses faye for realtime notifications. below is quick guide to subscribing to feed changes
// ⚠️ usertoken is generated server-side (see previous section)
final client = streamclient.connect('your_api_key', token: usertoken,appid: 'app_id');
final user1 = client.flatfeed('user', '1');
// subscribe to the changes
final subscription = await userfeed.subscribe((message) => print(message));
// now whenever something changes to the feed user 1
// the callback will be called
// to cancel a subscription you can call cancel on the
// object returned from a subscribe call.
// this will remove the listener from this channel.
await subscription.cancel();
docs are available on getstream.io.
Comments are closed.