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

Wskaźniki pisania i obecność online

Nauczą się Państwo dodawać do aplikacji czatu wskaźniki pisania w czasie rzeczywistym i obecność online, wykorzystując zdarzenia STOMP, heartbeat oraz śledzenie obecności w Spring.

Wskaźniki pisania i obecność 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.

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.

Często zadawane pytania

Czy lekcja „Wskaźniki pisania i obecność online” jest bezpłatna?

Tak — pełny tekst „Wskaźniki pisania i obecność 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 „Wskaźniki pisania i obecność online”?

Nauczą się Państwo dodawać do aplikacji czatu wskaźniki pisania w czasie rzeczywistym i obecność online, wykorzystując zdarzenia STOMP, heartbeat oraz śledzenie obecności w Spring. Ć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 „Wskaźniki pisania i obecność 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. Projektowanie modelu wiadomości czatu
  2. Implementowanie publicznych pokojów czatu
  3. Prywatne wiadomości z użyciem STOMP
  4. Wskaźniki pisania i obecność online
← Powrót do WebSockets & Real-Time Systems with Spring