0Pricing
WebSockets & Real-Time Systems with Spring · 课时

跟踪在线状态与 presence

构建实时在线状态系统,显示谁处于在线状态,广播连接和断开事件,并呈现用户的实时状态。

跟踪在线状态与 presence 是 CoddyKit 上的免费 WebSockets & Real-Time Systems with Spring 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Real-Time Systems with Spring 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Real-Time Systems with Spring 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 / SessionDisconnectEvent and 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

常见问题解答

「跟踪在线状态与 presence」课时是免费的吗?

是的 — 「跟踪在线状态与 presence」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Real-Time Systems with Spring 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Real-Time Systems with Spring 课程共包含 4 节课。

「跟踪在线状态与 presence」这节课中我会学到什么?

构建实时在线状态系统,显示谁处于在线状态,广播连接和断开事件,并呈现用户的实时状态。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Real-Time Systems with Spring,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Real-Time Systems with Spring 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Real-Time Systems with Spring 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「跟踪在线状态与 presence」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Real-Time Systems with Spring 课中编写并运行代码吗?

能。每节 WebSockets & Real-Time Systems with Spring 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 服务器发送事件(SSE)与 WebSockets 对比
  2. 实时数据推送架构
  3. 实现用户通知
  4. 跟踪在线状态与 presence
← 返回 WebSockets & Real-Time Systems with Spring