Presence Detection and User Status
You'll learn how to track which users are actively online and viewing their feeds so you can intelligently route real-time updates only where they'll be seen.
Why Presence Matters
Imagine broadcasting every new post to every user's device via WebSocket or SSE—even when most users aren't actively using the app. That wastes server resources, network bandwidth, and battery life. Presence detection lets you distinguish between "online and active" vs "offline" vs "connected but inactive," so you can optimize real-time delivery accordingly.
Core Presence Architecture
When a user opens the app:
- Heartbeat mechanism: Client sends periodic "I'm alive" pings (every 30–60 seconds) over the existing WebSocket/SSE connection
- Presence service: Tracks active user IDs in a fast key-value store (Redis) with TTL (time-to-live) expiration
- Status updates: When a user goes idle (no heartbeat), their presence key expires automatically
Example presence entry in Redis:
SET user:12345:presence "online" EX 90
(Expires in 90 seconds unless refreshed)
Smart Update Routing
The fanout service or pub-sub layer checks presence before pushing:
- Online users: Send real-time WebSocket updates immediately
- Offline users: Skip real-time push, they'll pull on next app open
- Idle users: Optional middle tier—defer updates briefly, then pull
This dramatically reduces unnecessary connection overhead. If only 10% of your users are active at peak, you route real-time updates to 10% of connections instead of 100%.
Status Granularity
Beyond binary online/offline, you can track:
- "Actively viewing feed" vs "app backgrounded"
- Last-seen timestamp for UX features
- Device type for optimized payload sizing
Key Takeaway:
Presence detection tracks active users via heartbeats and fast-expiring state, enabling you to route real-time feed updates only to online users—dramatically reducing server load and improving efficiency.