Typing Indicators & Online Presence
Learn to add real-time typing indicators and online presence to a chat app using STOMP events, heartbeats, and presence tracking in Spring.
Typing Indicators & Online Presence is a free WebSockets & Real-Time Systems with Spring lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Real-Time Systems with Spring learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Typing Indicators & Online Presence” lesson free?
Yes — the full text of “Typing Indicators & Online Presence” is free to read here on the web, and the WebSockets & Real-Time Systems with Spring course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Real-Time Systems with Spring course, upgrade to CoddyKit PRO.
What will I learn in “Typing Indicators & Online Presence”?
Learn to add real-time typing indicators and online presence to a chat app using STOMP events, heartbeats, and presence tracking in Spring. You practise WebSockets & Real-Time Systems with Spring with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Real-Time Systems with Spring?
No prior experience is required. WebSockets & Real-Time Systems with Spring on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Typing Indicators & Online Presence” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Real-Time Systems with Spring lesson?
Yes. Every WebSockets & Real-Time Systems with Spring lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Designing a Chat Message Model
- Implementing Public Chat Rooms
- Private Messaging with STOMP
- Typing Indicators & Online Presence