X-style home timeline
This recipe builds a home timeline like the one in X (formerly Twitter): a list of posts from the people a user follows, newest first, a "Show new posts" banner instead of posts jumping in while the user reads, likes, threaded replies, reposts and quote posts, and bookmarks.
Everything here uses two built-in feed groups, so there is no feed group to set up:
- Each user posts to their own
user:<user id>feed. - Each user reads their
timeline:<user id>feed, which follows theuserfeeds of the people they follow. Itsfollowingselector merges the posts of the followed feeds when the timeline is read, and caches the result per user, so later reads only add what's new.
1. Create each user's feeds
Create the user's user and timeline feeds when they sign up, by calling getOrCreate on user:<user id> and timeline:<user id> from your server.
2. Follow people
Follow from the user's timeline feed to the other user's user feed. On X, a user's own posts show up in their home timeline too, so also follow the user's own user feed from their timeline when they sign up. Use getOrCreateFollows: it's idempotent, so calling it for a follow that already exists does nothing, and it takes up to 1000 follows in one request.
// Jack's timeline follows Maria and his own user feed
await _feedsV3Client.GetOrCreateFollowsAsync(
new FollowBatchRequest
{
Follows = new List<FollowRequest>
{
new FollowRequest { Source = "timeline:jack", Target = "user:maria" },
new FollowRequest { Source = "timeline:jack", Target = "user:jack" }
}
}
);The self-follow counts like any other: it adds 1 to the user feed's follower_count and to the timeline's following_count, so subtract it when you show the counts.
To unfollow, call getOrCreateUnfollows with the same source and target, up to 100 in one request. See Follows.
3. Post
Add the post to the author's user feed. It shows up in the timelines of everyone who follows that feed. To limit who can reply, as X does, set restrict_replies to everyone (the default), people_i_follow or nobody. The author can always reply to their own post.
var activity = new AddActivityRequest
{
Type = "post",
Text = "apple stock will go up",
UserID = "jack",
Feeds = new List<string> { "user:jack" },
RestrictReplies = "people_i_follow" // Options: "everyone", "people_i_follow", "nobody"
};
var response = await _feedsV3Client.AddActivityAsync(activity);To mention other users, pass their IDs in mentioned_user_ids. See User mentions.
4. Show the timeline
Read the user's timeline and load the next page as they scroll. Three choices keep the read fast:
- Use a page size close to one screen. 20 posts is plenty for the first page.
- Skip what the list doesn't show. Skip comments enrichment and load comments when the user opens a post (step 7). Every activity still has its
comment_countfor the reply counter. Reactions, bookmarks and the parent of reposts stay on, since every post in the list shows them. - Watch the feed.
watch: truesubscribes the client to real-time events, so likes, replies and new posts arrive without reading the feed again (step 5).
const timeline = client.feed("timeline", "jack");
const readOptions = {
limit: 20,
watch: true,
enrichment_options: {
skip_activity_comments: true,
// Only needed to hide the reply button on restricted posts (step 7)
enrich_own_followings: true,
},
};
await timeline.getOrCreate(readOptions);
console.log(timeline.state.getLatestValue().activities);
// Load the next page when the user scrolls to the end
await timeline.getNextPage();The merged timeline is cached per user, so it's cheap to read, but avoid reading the first page again every time the app comes to the foreground. Keep the activities you already have and let real-time events update them.
5. Show a "Show new posts" banner
When someone the user follows posts while they read, X doesn't push the list down; it shows a "Show N posts" banner. The client SDK does the same by default: on a watched feed, only the user's own new posts are added to the list, and other users' new posts are left out of it.
To show a banner, pass an onNewActivity callback when you create the feed. Add the user's own posts to the top and count everyone else's. When the user taps the banner, read the first page again with the same options as in step 4, which brings in the new posts with all their enrichment. A timeline read is cached for 10 seconds, so a tap within 10 seconds of the previous read can return the page without the new posts.
let newPostCount = 0;
const timeline = client.feed("timeline", "jack", {
onNewActivity: ({ activity, currentUser }) => {
// Show the user's own posts right away
if (activity.user.id === currentUser?.id) return "add-to-start";
// Count everyone else's for the "Show N posts" banner
newPostCount += 1;
return "ignore";
},
});
// When the user taps the banner
newPostCount = 0;
await timeline.getOrCreate(readOptions);To get the events yourself, for example to show the new posts' authors' avatars on the banner, subscribe with timeline.on("feeds.activity.added", handler). See Activity list controls and Events.
6. Like and unlike
A like is a reaction of type like. A user holds at most one reaction of each type on a post, so liking a post again doesn't add a second like. Each activity has own_reactions to show whether the user liked it, and reaction_count and reaction_groups for the counter. On a watched timeline, the client updates them when anyone likes or unlikes the post, so you don't need to read the feed again.
// Add a reaction to an activity
var addResponse = await _feedsV3Client.AddActivityReactionAsync(
"activity_123",
new AddReactionRequest
{
Type = "like",
UserID = "jack"
}
);
var deleteResponse = await _feedsV3Client.DeleteActivityReactionAsync(
"activity_123",
"like",
new { user_id = "jack" }
);7. Reply
A reply is a comment on the activity. A reply to a reply is a comment with parent_id set to the comment it answers. If the post restricts replies, a user who isn't allowed to reply gets an error, so hide the reply button for them. With nobody, only the author can reply. With people_i_follow, a user can reply if the author follows them: read the timeline with enrich_own_followings: true (step 4), and show the button when the activity's current_feed.own_followings isn't empty.
// Adding a comment to an activity
await _feedsV3Client.AddCommentAsync(
new AddCommentRequest
{
Comment = "So great!",
ObjectID = "activity_123",
ObjectType = "activity",
UserID = "jack"
}
);
// Adding a reply to a comment
await _feedsV3Client.AddCommentAsync(
new AddCommentRequest
{
Comment = "I agree!",
ParentID = "comment_456",
UserID = "jack"
}
);Load the replies only when the user opens a post, and load the replies to a reply when the user expands it. loadNextPageActivityComments and loadNextPageCommentReplies store the comments in the feed's state, so they update in real time like the rest of the timeline.
// When the user opens a post, load its first page of replies
await timeline.loadNextPageActivityComments(activity, { limit: 20 });
const replies =
timeline.state.getLatestValue().comments_by_entity_id[activity.id]?.comments;
// When the user expands a reply, load the replies to it
await timeline.loadNextPageCommentReplies(comment);
const repliesToComment =
timeline.state.getLatestValue().comments_by_entity_id[comment.id]?.comments;See Comments for sorting, editing and deleting comments.
8. Repost and quote
A repost and a quote post are both new activities in the reposting user's user feed with parent_id set to the original. Stream increments the original's share_count, and timeline reads include the original as activity.parent, so the card needs no extra request. For a repost, use a type of your own such as repost and leave out text:
var activity = new AddActivityRequest
{
Type = "repost",
ParentID = "<activity to share>",
UserID = "jack",
Feeds = new List<string> { "user:jack" }
};
var response = await _feedsV3Client.AddActivityAsync(activity);For a quote post, post as in step 3 with the user's text and the same parent_id. To undo a repost, delete the repost activity; the original's share_count goes down by 1.
9. Bookmark
Bookmarks are private to the user. own_bookmarks on each activity shows whether the user bookmarked it.
var bookmark = await _feedsV3Client.AddBookmarkAsync(
"activity_123",
new AddBookmarkRequest { UserID = "jack" }
);To remove a bookmark, call deleteBookmark with the same activity ID. To show the user's bookmarks page, use queryBookmarks. See Bookmarks for both, and for bookmark folders.
Going further
- Add a For You tab. X's second tab is a ranked feed that mixes in posts from accounts the user doesn't follow. See the For You feed recipe.
- Notify authors. Pass
create_notification_activity: truewhen a user follows, likes, replies or mentions someone. See the notification inbox recipe. - Pin a post to a profile. See Pins.
- Add polls. See Polls.
- Show a profile page. Read the author's
userfeed with the same enrichment options. Itsfollower_countandfollowing_countare on the feed itself, so you don't need the followers and following lists to show the counts.