0Pricing
WebSockets & Real-Time Systems with Spring · レッスン

プレゼンスとオンライン状態の追跡

接続・切断イベントをブロードキャストし、ユーザーの現在の状態を表示するリアルタイムプレゼンスシステムを構築する方法を学びます。

「プレゼンスとオンライン状態の追跡」は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レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「プレゼンスとオンライン状態の追跡」レッスンは無料ですか?

はい。「プレゼンスとオンライン状態の追跡」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Real-Time Systems with Springコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。

「プレゼンスとオンライン状態の追跡」で何を学びますか?

接続・切断イベントをブロードキャストし、ユーザーの現在の状態を表示するリアルタイムプレゼンスシステムを構築する方法を学びます。 ブラウザで直接実行するハンズオンコードで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フィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Server-Sent Events(SSE)とWebSocketsの比較
  2. リアルタイムデータプッシュアーキテクチャ
  3. ユーザー通知の実装
  4. プレゼンスとオンライン状態の追跡
← WebSockets & Real-Time Systems with Springに戻る