0Pricing
WebSockets & Real-Time Systems with Spring · Lekcja

Śledzenie obecności i statusu online

Zbudują Państwo system obecności w czasie rzeczywistym, który pokazuje, kto jest online, rozgłasza zdarzenia połączenia i rozłączenia oraz prezentuje bieżący status użytkowników.

Śledzenie obecności i statusu online to bezpłatna lekcja WebSockets & Real-Time Systems with Spring na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej WebSockets & Real-Time Systems with Spring, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Śledzenie obecności i statusu online” jest bezpłatna?

Tak — pełny tekst „Śledzenie obecności i statusu online” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu WebSockets & Real-Time Systems with Spring, przejdź na CoddyKit PRO. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.

Co nauczysz się w „Śledzenie obecności i statusu online”?

Zbudują Państwo system obecności w czasie rzeczywistym, który pokazuje, kto jest online, rozgłasza zdarzenia połączenia i rozłączenia oraz prezentuje bieżący status użytkowników. Ćwiczysz WebSockets & Real-Time Systems with Spring z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć WebSockets & Real-Time Systems with Spring?

Nie wymagamy żadnego doświadczenia. WebSockets & Real-Time Systems with Spring w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Śledzenie obecności i statusu online”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji WebSockets & Real-Time Systems with Spring?

Tak. Każda lekcja WebSockets & Real-Time Systems with Spring zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Server-Sent Events (SSE) a WebSockets
  2. Architektury przesyłania danych w czasie rzeczywistym
  3. Implementowanie powiadomień użytkowników
  4. Śledzenie obecności i statusu online
← Powrót do WebSockets & Real-Time Systems with Spring