How to Build a Real-Time Presence Indicator for Your Website
The Problem: Users See Avatars but Don't Know Who's Currently Online
We worked on a project — an online course platform with live webinars. Participants complained: "I write to the teacher in the chat, but they don't answer — turns out they're no longer online." It might seem trivial, but the lack of a real-time indicator reduced engagement by 15%. We implemented a presence indicator in two days. Now everyone sees a green dot and knows whom to ask a question right now. Over three years, we've deployed more than 50 such solutions for chats, courses, and corporate portals.
Heartbeat via HTTP Instead of WebSocket
We use heartbeat via HTTP — we send POST /api/presence/ping every 30 seconds. If there's no ping for 90 seconds, the user is considered offline. This approach is simpler than WebSocket and doesn't require a persistent connection. For most websites, it's sufficient. WebSocket provides accuracy down to a second but requires server infrastructure and a constant connection. The choice depends on the scenario: for a chat — WebSocket, for a course participant list — HTTP heartbeat.
| Approach | Accuracy | Complexity | When to Use |
|---|---|---|---|
| WebSocket/SSE | ~1 second | High | Chat, collaborative editing |
| HTTP heartbeat | ~60 seconds | Low | Profiles, participant lists |
| Last seen | ~3 minutes | Very low | Privacy settings |
Redis as the Ideal Storage for Presence
Redis is 10× faster than PostgreSQL in write speed for this task and automatically removes stale data without cron jobs. Each setex call creates a key with a TTL of 90 seconds. If the user stops pinging, the key disappears automatically. As noted in the Redis documentation, the SETEX command sets a key with automatic expiration. Below is a simplified implementation of our service:
class PresenceService { private const TTL = 90; public function markOnline(int $userId, string $context = 'global'): void { Redis::setex("presence:{$context}:{$userId}", self::TTL, now()->timestamp); $wasOnline = Redis::exists("presence_flag:{$context}:{$userId}"); if (!$wasOnline) { Redis::setex("presence_flag:{$context}:{$userId}", self::TTL + 10, 1); broadcast(new UserCameOnline($userId, $context)); } } public function markOffline(int $userId, string $context = 'global'): void { Redis::del("presence:{$context}:{$userId}"); Redis::del("presence_flag:{$context}:{$userId}"); broadcast(new UserWentOffline($userId, $context)); } public function getOnlineUsers(string $context = 'global'): array { $keys = Redis::keys("presence:{$context}:*"); return array_map(fn($k) => (int) last(explode(':', $k)), $keys); } public function isOnline(int $userId, string $context = 'global'): bool { return (bool) Redis::exists("presence:{$context}:{$userId}"); } } Explanation of the code
The `$context` parameter allows splitting presence across sections: `chat_room:42`, `course:17`, `global`. The `setex` command sets a key that expires after TTL seconds. The `presence_flag` prevents duplicate broadcast events on each ping.The $context parameter allows splitting presence across sections: chat_room:42, course:17, global.
Broadcast Events Synchronize Status
When the status changes, we broadcast UserCameOnline and UserWentOffline events. They contain only the user ID and context. The client receives the event and updates the green dot. For Laravel broadcast we use Pusher or Redis + Socket.IO. Example event:
class UserCameOnline implements ShouldBroadcast { public $userId; public $context; public function broadcastOn(): array { return [new PresenceChannel("presence.{$this->context}")]; } } The client subscribes to the channel via Laravel Echo and reacts to messages.
Why Choose a TTL of 90 Seconds?
The TTL should be three times the ping interval to compensate for brief connection losses. With a ping every 30 seconds, a TTL of 90 seconds covers three missed pings. If the connection drops for 40 seconds, the user doesn't go offline. A shorter TTL (e.g., 60 seconds) causes status flickering under unstable networks. A longer TTL (120+ seconds) delays the detection of user departure.
| TTL | Behavior |
|---|---|
| 60 s | flickers after missing 2 pings |
| 90 s | stable, reacts after 3 missed |
| 120 s | slow offline detection |
Step-by-Step Instructions for Implementing a Heartbeat Ping
- Create a POST endpoint with authentication (e.g., Sanctum).
- In the PresenceService, implement
markOnline/markOfflinemethods. - On the client, send a ping on page load, then repeat every 30 seconds using
setInterval. - In the
beforeunloadhandler, send an offline request vianavigator.sendBeacon(see MDN documentation). - On the server, upon receiving a ping, update the TTL. If no ping for 90 seconds, Redis deletes the key and an offline event is sent (implement a check on each ping or use a background task).
Common Mistakes When Implementing a Presence Indicator
- Not using
sendBeacon— when the tab is closed, the request doesn't go out, and the user stays online until TTL expires. Solution: usenavigator.sendBeacon. - No context separation — all users see each other regardless of section. Solution: pass
contextin every ping. - TTL too short — the user flickers (online/offline) under unstable connections. We recommend TTL = 3 times the ping interval.
What's Included in the Work
- Development of the heartbeat endpoint and Redis service
- Configuration of broadcast events
UserCameOnline,UserWentOffline - Implementation of the indicator in the UI (dot / badge)
- API and integration documentation
- Team instruction for further maintenance
Our engineers have 5+ years of experience with Laravel and Vue/React. Over 3 years, we have implemented more than 50 similar solutions for chats, courses, and corporate portals. Our proven track record guarantees reliable implementation.
Timelines and Cost
- Heartbeat ping + Redis TTL + indicator: 1–2 days (approx. $1,500–$3,000)
- Broadcast on status change: 1 day (approx. $1,000)
- Presence Channels via Laravel Echo: 1 day (approx. $1,000)
- Last seen: 0.5 day (approx. $500)
- Privacy settings: +0.5 day (approx. $500)
Cost is calculated individually based on complexity. Contact us — we'll evaluate your project within one business day. Get a consultation for your project — our engineers will help select the optimal solution. We offer a 100% satisfaction guarantee.







