Activity Feeds V3 is in closed alpha — do not use it in production (just yet).

Feeds

Creating a Feed

// Feed with no extra fields, of feed group "user"
final feed = client.feed(group: 'user', id: 'john');
await feed.getOrCreate();

// More options
const query = FeedQuery(
  fid: FeedId(group: 'user', id: 'jack'),
  data: FeedInputData(
    description: 'My personal feed',
    name: 'jack',
    visibility: FeedVisibility.public,
  ),
);

final feed2 = client.feedFromQuery(query);
await feed2.getOrCreate();

Reading a Feed

Here is a basic example of how to read a feed:

final feed = client.feed(group: 'user', id: 'john');
await feed.getOrCreate();
final feedData = feed.state.feed;
final activities = feed.state.activities;
final members = feed.state.members;

// Note: Always dispose the feed when you are done with it
feed.dispose();

The response will contain the following data.

You have more options when reading a feed, let’s go over a few:

const query = FeedQuery(
  fid: FeedId(group: 'user', id: 'john'),
  // filter activities with filter tag green
  activityFilter: Filter.in_(
    ActivitiesFilterField.filterTags,
    ['green'],
  ),
  activityLimit: 10,
  // additional data used for ranking
  externalRanking: {'user_score': 0.8},
  followerLimit: 10,
  followingLimit: 10,
  memberLimit: 10,
  // overwrite the default ranking or aggregation logic for this feed. good for split testing
  view: 'myview',
  // receive web-socket events with real-time updates
  watch: true,
);

final feed = client.feedFromQuery(query);
await feed.getOrCreate();
final activities = feed.state.activities;
final feedData = feed.state.feed;

// Note: Always dispose the feed when you are done with it
feed.dispose();

Feed Pagination

Here is how you can read the next page on the feed:

final feed = client.feedFromQuery(
  const FeedQuery(
    fid: FeedId(group: 'user', id: 'john'),
    activityLimit: 10,
  ),
);

// Page 1
await feed.getOrCreate();
final activities = feed.state.activities; // First 10 activities

// Page 2
final page2Activities = await feed.queryMoreActivities(limit: 10);

final page1And2Activities = feed.state.activities;

Filtering Examples

Another common use case is filtering a feed. This is trickier than it seems. Keep in mind that for performance reasons feeds often have to be computed on write time. To allow for filtering we expose the following API.

// Add a few activities
const feedId = FeedId(group: 'user', id: 'john');
await client.upsertActivities(
  activities: [
    ActivityRequest(
      feeds: [feedId.rawValue],
      filterTags: const ['green', 'blue'],
      text: 'first',
      type: 'post',
    ),
    ActivityRequest(
      feeds: [feedId.rawValue],
      filterTags: const ['yellow', 'blue'],
      text: 'second',
      type: 'post',
    ),
    ActivityRequest(
      feeds: [feedId.rawValue],
      filterTags: const ['orange'],
      text: 'third',
      type: 'post',
    ),
  ],
);
// Now read the feed, this will fetch activity 1 and 2
const query = FeedQuery(
  fid: feedId,
  activityFilter: Filter.in_(ActivitiesFilterField.filterTags, ['blue']),
);

final feed = client.feedFromQuery(query);
await feed.getOrCreate();
// contains first and second
final activities = feed.state.activities;

The filter syntax also supports $or and $and, so here’s an example that’s a little more complicated:

// Get all the activities where filter tags contain both "green" and "orange"
const query = FeedQuery(
  fid: FeedId(group: 'user', id: 'john'),
  activityFilter: Filter.and([
    Filter.in_(ActivitiesFilterField.filterTags, ['green']),
    Filter.in_(ActivitiesFilterField.filterTags, ['orange']),
  ]),
);

final feed = client.feedFromQuery(query);
await feed.getOrCreate();
final activities = feed.state.activities;

Feed Members

You can add and remove members to a feed. This is useful for building communities where a set of users can add content to the feed.

It’s not possible to set/update member role on client-side. Use server-side SDKs for this. When adding members client-side all new members will have feed_member role:

// The following methods are available to edit the members of a feed
final result = await feed.updateFeedMembers(
  request: const UpdateFeedMembersRequest(
    members: [
      FeedMemberRequest(
        custom: {'joined': '2024-01-01'},
        role: 'moderator',
        userId: 'john',
      ),
    ],
    operation: UpdateFeedMembersRequestOperation.upsert,
  ),
);

// Remove members
await feed.updateFeedMembers(
  request: const UpdateFeedMembersRequest(
    members: [
      FeedMemberRequest(userId: 'john'),
      FeedMemberRequest(userId: 'jane'),
    ],
    operation: UpdateFeedMembersRequestOperation.remove,
  ),
);

// Set members (overwrites the list)
await feed.updateFeedMembers(
  request: const UpdateFeedMembersRequest(
    members: [FeedMemberRequest(role: 'moderator', userId: 'john')],
    operation: UpdateFeedMembersRequestOperation.set,
  ),
);

Feed members vs followers

Followers and members might seem like similar concepts, but they serve two different purposes with some key differences.

Followers can only be feeds (for example the timeline feed of Alice follows the user feed of Bob). Followers’ aim is to access the content of a feed they’re interested in and interact with it.

Members can only be users (for example Alice adds Bob as a member to her feed about “Travel Hacks”). The aim of feed members is usually to help out with admin tasks (helpful if you want to build apps similar to Facebook pages) or to decide what activities a user has access to using membership levels (for example Bob becomes a premium member in Alice’s community).

Member invites

You can invite members with the invite flag, where invited users can accept or reject the membership.

// Request to become a member
await feed.updateFeedMembers(
  request: const UpdateFeedMembersRequest(
    members: [
      FeedMemberRequest(
        custom: {'reason': 'community builder'},
        invite: true,
        role: 'moderator',
        userId: 'john',
      ),
    ],
    operation: UpdateFeedMembersRequestOperation.upsert,
  ),
);

// Accept and reject member requests
await feed.acceptFeedMember();
await feed.rejectFeedMember();

Query Feeds

Querying feeds allows you to do things like showing the list of communities you’ve joined.

Here’s an example of how to query feeds:

Querying My Feeds

final query = FeedsQuery(
  filter: const Filter.equal(FeedsFilterField.createdById, 'john'),
  sort: [FeedsSort.desc(FeedsSortField.createdAt)],
  limit: 10,
  watch: true,
);

final feedList = client.feedList(query);

// Page 1
final page1 = await feedList.get();

// Page 2
final page2 = await feedList.queryMoreFeeds(limit: 10);

final page1And2 = feedList.state.feeds;

Querying Feeds Where I Am a Member

const query = FeedsQuery(
  filter: Filter.contains(FeedsFilterField.members, 'john'),
);
final feedList = client.feedList(query);
final feeds = await feedList.get();

Querying feeds by name or visibility

const sportsQuery = FeedsQuery(
  filter: Filter.and([
    Filter.equal(FeedsFilterField.visibility, 'public'),
    Filter.query(FeedsFilterField.name, 'Sports'),
  ]),
);

final sportsFeedList = client.feedList(sportsQuery);
final sportsFeeds = await sportsFeedList.get();

const techQuery = FeedsQuery(
  filter: Filter.and([
    Filter.equal(FeedsFilterField.visibility, 'public'),
    Filter.autoComplete(FeedsFilterField.description, 'tech'),
  ]),
);

final techFeedList = client.feedList(techQuery);
final techFeeds = await techFeedList.get();

Querying feeds by creator name

const query = FeedsQuery(
  filter: Filter.and([
    Filter.equal(FeedsFilterField.visibility, 'public'),
    Filter.query(FeedsFilterField.createdByName, 'Thompson'),
  ]),
);

final feedList = client.feedList(query);
final feeds = await feedList.get();

Feeds Queryable Built-in Fields

nametypedescriptionsupported operationsexample
idstring or list of stringsThe ID of the feed$in, $eq{ id: { $in: [ 'abc', 'xyz' ] } }
group_idstring or list of stringsThe ID of the group this feed belongs to$in, $eq{ group_id: { $in: [ 'abc', 'xyz' ] } }
feedstring or list of stringsThe fully qualified feed ID (group_id:id)$in, $eq{ fid: { $in: [ 'abc', 'xyz' ] } }
visibilitystring or list of stringsThe visibility setting of the feed$in, $eq{ visibility: { $eq: 'public' } }
created_by_idstring or list of stringsThe ID of the user who created the feed$in, $eq{ created_by_id: { $in: [ 'abc', 'xyz' ] } }
created_by.namestringThe name of the user who created the feed$eq, $q, $autocomplete{ 'created_by.name': { $autocomplete: 'Frank' } }
namestringThe name of the feed$eq, $q, $autocomplete{ name: { $q: 'Sports' } }
descriptionstringThe description of the feed$eq, $q, $autocomplete{ description: { $autocomplete: 'tech' } }
member_countnumberThe number of members in this feed$eq, $ne, $gt, $lt, $gte, $lte{ member_count: { $gt: 100 } }
memberslist of stringsThe list of members in this feed$in{ members: { $in: [ 'bob', 'alice' ] } }
following_countnumberThe number of feeds this feed follows$eq, $ne, $gt, $lt, $gte, $lte{ following_count: { $gt: 100 } }
following_feedslist of stringsThe list of feeds this feed follows$in{ following_feeds: { $in: [ 'feed1', 'feed2' ] } }
follower_countnumberThe number of followers of this feed$eq, $ne, $gt, $lt, $gte, $lte{ follower_count: { $gt: 100 } }
created_atstring, RFC3339 timestampThe time the feed was created$eq, $gt, $lt, $gte, $lte{ created_at: { $gte: '2023-12-04T09:30:20.45Z' } }
updated_atstring, RFC3339 timestampThe time the feed was updated$eq, $gt, $lt, $gte, $lte{ updated_at: { $gte: '2023-12-04T09:30:20.45Z' } }

Feeds can be sorted by created_at, updated_at, member_count, follower_count, and following_count.

Be sure to reach out to support if you need additional query feed capabilities.

© Getstream.io, Inc. All Rights Reserved.