入力中インジケーターとオンラインプレゼンス
SpringでSTOMPイベント、ハートビート、プレゼンス追跡を使い、チャットアプリにリアルタイムの入力中インジケーターとオンラインプレゼンスを追加する方法を学びます。
「入力中インジケーターとオンラインプレゼンス」はCoddyKit上の無料WebSockets & Real-Time Systems with Springレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「入力中インジケーターとオンラインプレゼンス」レッスンは無料ですか?
はい。「入力中インジケーターとオンラインプレゼンス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Real-Time Systems with Springコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。
「入力中インジケーターとオンラインプレゼンス」で何を学びますか?
SpringでSTOMPイベント、ハートビート、プレゼンス追跡を使い、チャットアプリにリアルタイムの入力中インジケーターとオンラインプレゼンスを追加する方法を学びます。 ブラウザで直接実行するハンズオンコードでWebSockets & Real-Time Systems with Springを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- チャットメッセージモデルの設計
- パブリックチャットルームの実装
- STOMPによるプライベートメッセージング
- 入力中インジケーターとオンラインプレゼンス