Tracking Presence and Online Status
Build a real-time presence system that shows who is online, broadcasting connect/disconnect events and surfacing live user status.
Tracking Presence and Online Status is a free WebSockets & Real-Time Systems with Spring lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Real-Time Systems with Spring learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Presence?
Presence is knowing who is currently online: the green dots in chat apps, the live cursors in collaborative editors, the active-now lists. It is one of the most requested real-time features.
Presence Is About Lifecycle Events
The core insight: presence is derived from connection lifecycle. When a session opens, mark the user online; when it closes, mark them offline, then broadcast the change.
Listening to Connect Events
Spring publishes a SessionConnectedEvent when a STOMP session is established. Use it to record the user as online.
@EventListener
public void onConnect(SessionConnectedEvent e) {
String user = e.getUser().getName();
presence.markOnline(user);
broadcast(user, "ONLINE");
}Listening to Disconnect Events
A matching SessionDisconnectEvent fires on close. Mark the user offline and broadcast it.
@EventListener
public void onDisconnect(SessionDisconnectEvent e) {
String user = userFor(e.getSessionId());
presence.markOffline(user);
broadcast(user, "OFFLINE");
}Storing Presence State
For a single server a concurrent map works. For a cluster, store presence in Redis so every node sees the same online set.
redis.opsForSet().add("online:users", username);
redis.opsForValue().set("presence:" + username, "ONLINE", Duration.ofMinutes(2));Broadcasting Status Changes
Push each change to a presence topic so every interested client updates its UI live.
messagingTemplate.convertAndSend("/topic/presence",
new PresenceEvent(username, status));Multiple Tabs and Devices
A user with two tabs has two sessions. Reference-count them: only mark offline when the last session closes, or you will flicker the status.
Handling Ungraceful Disconnects
A crash may not send a clean close. Pair presence with heartbeats and a TTL: if no heartbeat arrives before the key expires, treat the user as offline.
Last-Seen Timestamps
Beyond online/offline, record a last-seen time on disconnect. The UI can then show "active 5 minutes ago" for offline users.
redis.opsForValue().set("lastseen:" + username, Instant.now().toString());Sending the Initial Snapshot
When a client first connects it needs the current online list, not just future changes. Send a snapshot on subscription, then stream deltas.
@SubscribeMapping("/presence.init")
public Set<String> initial() {
return presence.onlineUsers();
}Privacy Considerations
Presence can leak behavior patterns. Offer an invisible mode and respect it server-side, so a user who opts out is never broadcast as online.
Quick Check
Test your presence knowledge.
Recap
You built presence:
- Presence derives from connect/disconnect lifecycle events
- Use
SessionConnectedEvent/SessionDisconnectEventand broadcast changes - Store shared state in Redis for clustering; reference-count multiple sessions
- Pair with heartbeats and TTLs to catch ungraceful disconnects
- Send an initial snapshot, track last-seen, and honor invisible mode
Frequently asked questions
Is the “Tracking Presence and Online Status” lesson free?
Yes — the full text of “Tracking Presence and Online Status” is free to read here on the web, and the WebSockets & Real-Time Systems with Spring course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Real-Time Systems with Spring course, upgrade to CoddyKit PRO.
What will I learn in “Tracking Presence and Online Status”?
Build a real-time presence system that shows who is online, broadcasting connect/disconnect events and surfacing live user status. You practise WebSockets & Real-Time Systems with Spring with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Real-Time Systems with Spring?
No prior experience is required. WebSockets & Real-Time Systems with Spring on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tracking Presence and Online Status” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Real-Time Systems with Spring lesson?
Yes. Every WebSockets & Real-Time Systems with Spring lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Server-Sent Events (SSE) vs. WebSockets
- Real-Time Data Push Architectures
- Implementing User Notifications
- Tracking Presence and Online Status