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

输入状态指示与在线状态

学习在聊天应用中使用 STOMP 事件、心跳和 Spring 中的在线状态跟踪,添加实时输入状态指示和在线状态功能。

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

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

Beyond Messages

Great chat apps do more than deliver messages. Typing indicators and online presence make conversations feel live and responsive.

This lesson adds both features on top of your existing STOMP chat.

What Is Presence?

Presence tells users who is currently online, away, or offline. It is updated in real time as users connect and disconnect, giving the social cue that someone is available to chat.

Tracking Connections

Spring publishes SessionConnectedEvent and SessionDisconnectEvent. Listen for them to maintain a live set of online users.

@EventListener
public void onConnect(SessionConnectedEvent event) {
    String user = userFrom(event);
    onlineUsers.add(user);
    broadcastPresence(user, 'ONLINE');
}

Handling Disconnects

When a user disconnects, remove them and notify others so the presence list stays accurate.

@EventListener
public void onDisconnect(SessionDisconnectEvent event) {
    String user = userFrom(event);
    onlineUsers.remove(user);
    broadcastPresence(user, 'OFFLINE');
}

Broadcasting Presence

Send presence updates to a shared topic so every connected client can update its UI.

private void broadcastPresence(String user, String status) {
    Map<String, String> event = new HashMap<>();
    event.put('user', user);
    event.put('status', status);
    messagingTemplate.convertAndSend('/topic/presence', event);
}

A Typing Event Model

Typing indicators are lightweight events, not stored messages. Define a small payload carrying the sender, the room, and whether they started or stopped typing.

{
  "type": "TYPING",
  "room": "general",
  "sender": "alice",
  "typing": true
}

Receiving Typing Events

Map a destination for typing events and relay them to the room topic without persisting them.

@MessageMapping('/chat.typing')
public void typing(TypingEvent event) {
    messagingTemplate.convertAndSend(
        '/topic/room.' + event.getRoom() + '.typing', event);
}

Debouncing on the Client

Clients should not send a typing event for every keystroke. Debounce: send 'typing' once, then send 'stopped' after a short idle period. This reduces traffic dramatically.

let timer = null;
function onKeyPress() {
  sendTyping(true);
  clearTimeout(timer);
  timer = setTimeout(function () { sendTyping(false); }, 2000);
}

Heartbeats for Stale Sessions

Network drops do not always fire a disconnect event. Use STOMP heartbeats so the server can detect dead connections and mark those users offline, keeping presence accurate.

Scaling Presence

With multiple server instances, presence must be shared. Store online users in a central store like Redis and broadcast changes across instances so every client sees a consistent presence list.

Privacy Considerations

Presence and typing reveal user activity. Let users control visibility, and only share presence with people who should see it (for example, contacts or members of the same room), not everyone on the server.

Quick Check

Test your understanding of presence and typing indicators.

Recap

You learned to add online presence using Spring's session connect and disconnect events and typing indicators as lightweight, non-persisted STOMP events. Debouncing, heartbeats, a shared store for scaling, and privacy controls make these features production-ready.

常见问题解答

「输入状态指示与在线状态」课时是免费的吗?

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

「输入状态指示与在线状态」这节课中我会学到什么?

学习在聊天应用中使用 STOMP 事件、心跳和 Spring 中的在线状态跟踪,添加实时输入状态指示和在线状态功能。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Real-Time Systems with Spring,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「输入状态指示与在线状态」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 设计聊天消息模型
  2. 实现公共聊天室
  3. 使用 STOMP 实现私信
  4. 输入状态指示与在线状态
← 返回 WebSockets & Real-Time Systems with Spring