A notification inbox looks like the simplest screen in a social app. Then you try to build one like Instagram's and run into three problems:
- Grouping. Five likes on the same photo should be one row: "alex.morgan and 4 others liked your photo", not five rows.
- The badge. The red dot on the bell has to appear the moment something happens, and clear in every open tab and device as soon as you open the inbox in one of them.
- Seen vs. read. Opening the inbox clears the badge. Tapping a notification clears its own dot. Those are two different states.
I built a complete, working version of this in Next.js with Stream Feeds, and this post walks through how each piece works, with the actual code.
Try the live demo → · Source on GitHub →
In the demo, every visitor gets their own account. Tap Demo, have five demo friends like your video, and watch the bell light up and the likes arrive as one grouped notification, live.
The stack
- Next.js 16 (App Router) with Tailwind CSS
- Stream Feeds for everything notification-related:
@stream-io/feeds-react-sdkin the browser and@stream-io/node-sdkon the server
Stream gives every user a few feeds. This app uses three built-in ones:
| Feed | What it holds |
|---|---|
user:<id> | The user's own posts. This is what friends like and comment on. |
timeline:<id> | The home feed. It follows friends' user feeds. |
notification:<id> | The user's notifications, already grouped, with read and seen tracking. |
You don't have to configure the notification feed. It comes with grouping and seen/read tracking turned on.
Step 1: Sign user tokens on the server
Your Stream API key is public. Your API secret stays on the server and signs a token for each user. In Next.js that's a route handler:
// app/api/token/route.ts import { NextResponse } from "next/server"; import { StreamClient } from "@stream-io/node-sdk"; const client = new StreamClient(process.env.STREAM_API_KEY!, process.env.STREAM_API_SECRET!); export async function GET() { const userId = await getSignedInUserId(); // your auth const token = client.generateUserToken({ user_id: userId, validity_in_seconds: 60 * 60 }); return NextResponse.json({ token }); } Step 2: Connect and watch the notification feed
In the browser, create the client with a tokenProvider that calls that route, then load the notification feed with watch: true. Watching is what makes everything live: new notifications, counts and read/seen changes arrive over WebSocket.
"use client"; import { useEffect, useMemo } from "react"; import { StreamFeeds, useCreateFeedsClient } from "@stream-io/feeds-react-sdk"; async function tokenProvider() { const res = await fetch("/api/token"); return (await res.json()).token; } export function App({ apiKey, user }) { const client = useCreateFeedsClient({ apiKey, userData: user, tokenOrProvider: tokenProvider }); const notifications = useMemo(() => client?.feed("notification", user.id), [client, user.id]); useEffect(() => { notifications?.getOrCreate({ limit: 20, watch: true }); }, [notifications]); if (!client || !notifications) return null; return ( <StreamFeeds client={client}> <NotificationBadge feed={notifications} /> <Inbox feed={notifications} /> </StreamFeeds> ); } Step 3: Create notifications
You never write to the notification feed yourself. When someone likes, comments or follows, pass create_notification_activity: true, and Stream adds a notification to the right person's feed:
// A like → the post's author gets "liked your photo" await client.feeds.addActivityReaction({ activity_id: post.id, type: "like", user_id: liker.id, create_notification_activity: true, }); // A follow → the followed user gets "followed you" await client.feeds.follow({ source: `timeline:${follower.id}`, target: `user:${userId}`, create_notification_activity: true, }); The same flag works from the client SDK, so when a user likes a friend's photo in the app, the friend is notified the same way.
Grouping: "alex.morgan and 4 others liked your photo"
This is the part that's genuinely hard to build yourself: a job that buckets events by post, type and day, merges new events into existing groups, and counts distinct people.
Stream does it on the server. The built-in notification feed groups with this format:
{{ target_id }}_{{ type }}_{{ time.strftime('%Y-%m-%d') }} So all likes on the same post on the same day become one group, which is how Instagram does it too. On the client, useAggregatedActivities returns the groups, and each one already has what a row needs:
| Field | Meaning |
|---|---|
activities | The notifications in the group, newest first |
activity_count | How many notifications are in the group |
user_count | How many different people caused them |
is_read, is_seen | Read and seen state for the whole group |
Turning a group into a sentence takes a few lines:
import { useAggregatedActivities } from "@stream-io/feeds-react-sdk"; function Inbox({ feed }) { const { aggregated_activities: groups = [] } = useAggregatedActivities(feed) ?? {}; return groups.map((group) => <NotificationRow key={group.group} group={group} />); } function describe(group) { const latest = group.activities[0]; // newest first const name = latest.user.name; // "alex.morgan" const others = group.user_count - 1; // distinct people, not activities const action = latest.type === "reaction" ? "liked your photo" : latest.type === "follow" ? "followed you" : "commented on your photo"; return others > 0 ? `${name} and ${others} others ${action}` : `${name} ${action}`; } Use user_count, not activity_count, for "and 12 others": someone who comments three times is three activities but one person.
Want different grouping, like "48 likes on 15 posts"? Change the format on the feed group (for example {{ type }}_{{ time.strftime('%Y-%m-%d') }}). The demo has a "Behind the scenes" panel that previews by post, by type and by person side by side.
The badge that stays in sync
useNotificationStatus(feed) returns unseen and unread. Because the feed is watched, Stream pushes every change, so this is the whole badge:
import { useNotificationStatus } from "@stream-io/feeds-react-sdk"; function NotificationBadge({ feed }) { const { unseen = 0 } = useNotificationStatus(feed) ?? {}; if (unseen === 0) return null; return <span className="badge" aria-label={`${unseen} new`} />; } No polling and no store of your own. Open the inbox in one tab, and the dot disappears in every other tab and device.
Seen vs. read
These are easy to mix up, and getting them right is what makes an inbox feel like Instagram's:
- Seen = the person opened the inbox. It drives the badge on the bell.
- Read = they tapped that notification. It drives the dot on each row.
Both are one call, feed.markActivity, and Stream broadcasts the new state everywhere:
const { unseen = 0 } = useNotificationStatus(feed) ?? {}; // Opening the inbox marks everything seen (clears the badge), // including notifications that arrive while it's open. useEffect(() => { if (unseen > 0) feed.markActivity({ mark_all_seen: true }); }, [feed, unseen]); // Tapping a notification marks that group read (clears its dot). function open(group) { if (!group.is_read) feed.markActivity({ mark_read: [group.group] }); } // "Mark all as read" <button onClick={() => feed.markActivity({ mark_all_read: true })}>Mark all as read</button> A detail worth stealing: "Since you were last here"
When you open the inbox after a while, the demo sums up what's new in one sentence: "Your video got 5 likes, priya.nair commented, and olive.fields followed you." useNotificationStatus also returns last_seen_at, so you can count what's newer than that before marking it seen:
const { last_seen_at } = useNotificationStatus(feed) ?? {}; const fresh = groups .filter((g) => !g.is_seen) .flatMap((g) => g.activities) .filter((a) => !last_seen_at || new Date(a.created_at) > new Date(last_seen_at)); const likes = fresh.filter((a) => a.type === "reaction").length; What you'd build yourself otherwise
| What it needs | Build it yourself | With Stream Feeds |
|---|---|---|
| A notification for the right person on every action | A row per recipient, in the request or a job queue | create_notification_activity: true |
| "Alex and 12 others" | A grouping job; merge new events; count distinct people | Built-in aggregation with user_count |
| No duplicates on like → unlike → like | Track who triggered what | Deduplication on by default |
| Unseen badge and unread dots | Per-user state per group, plus counters | markActivity and useNotificationStatus |
| Live in every tab and device | A WebSocket server with auth, reconnects and fan-out | getOrCreate({ watch: true }) |
Your server still signs tokens and decides who may do what. Stream stores, groups and delivers.
Run it yourself
git clone https://github.com/ivyzhang-svg/instagram-style-notification-feed.git cd instagram-style-notification-feed && npm install && cp .env.example .env.local Add your STREAM_API_KEY and STREAM_API_SECRET from the Stream dashboard (a free account is enough), then npm run dev.
The repo also has two drop-in components (a bell with a live count and an inbox), a FAQ covering pagination, custom notification types and adding this to an existing app, and the rest of the photo app: home timeline, posting, threaded comments, follows and a profile, all on Stream Feeds.
Live demo · GitHub · Stream Feeds docs
If you build something with it, I'd love to see it in the comments.



