Most teams add presence indicators by polling is_online every few seconds and showing green dots when the value is true. That approach lies to users within thirty seconds of a WebSocket disconnect, breaks completely under concurrent sessions, and shows "online now" for someone who left hours ago. Handle connection state, session multiplicity, and timezone-aware timestamps or the feature becomes a credibility problem.

The mistake almost everyone makes with presence

Most teams track presence by checking whether the user is "online" - a boolean flag stored in the database, flipped when they open the app and flipped back when they close it. Works fine until the app crashes, the user loses signal, or their phone dies. The flag stays true because the disconnect never fired.

You end up with ghost presence: users marked online who left long ago. Messages sit unread. Trust in the feature drops.

The trap is treating presence as state you write once and trust. Presence is a connection assertion that decays unless renewed. If you are not checking staleness, you are showing fiction.

This is the same core problem as typing indicators - a signal that must expire if not refreshed - but presence lives longer and fails more visibly.

A green dot next to a contact who ghosted you three meetings ago is not a feature. It is a liability.

Better disconnect logic will not fix this. Assume every presence claim will go stale and design around that from the start.

Why polling and heartbeats backfire

Most teams add a 30-second heartbeat when presence stops updating. The app pings the server every half-minute to prove the connection is alive. Feels like insurance against stale state.

Creates three new problems while solving none of the old ones.

First, the network round-trip adds latency to every status change. A user going offline now takes 30 seconds to reflect in the UI because you are waiting for the heartbeat to miss. Long enough for another user to send a message to someone who left.

Second, mobile apps background themselves without warning. iOS and Android suspend network activity when the user switches apps or locks the screen. Your heartbeat stops firing, but the WebSocket does not disconnect cleanly. The server marks the user offline after the timeout, then marks them online again when the app resumes - even if the user never opened it. You end up with phantom online status that flickers on and off as the OS schedules background tasks.

Third, you are polling connections that are working fine. Every active session sends a ping it does not need, every 30 seconds, forever. At 10,000 concurrent users that is 20 million pings a day - most of them confirming what the connection state already told you.

The same logic that makes read receipts brittle makes presence brittle. You are treating network state as something you check with timers instead of something the protocol surfaces directly. The WebSocket emits close and error events when the connection dies. Use those.

Heartbeats feel safer than trusting the socket. They add overhead, delay real disconnects, and lie when mobile apps background.

What to do instead: connection state plus graceful timeout

Track the WebSocket connection itself, not an artificial heartbeat on top of it. When a user's socket closes, mark them offline immediately. When it stays open but silent, wait a fixed grace period before marking them away.

We used this pattern in Tiny Talk Hub. The connection state tells you what you need to know. If the socket drops, the user is gone. If it hangs open without traffic, they left the app in the background or their device lost network quietly.

The timeout handles the edge case where the connection looks alive but the user walked away. Set it to 60-90 seconds. If no message arrives in that window, flip the status to away. When they send anything, reset the timer.

This works for custom messaging platforms where you control both client and server. The offline transition stays accurate without flooding your backend with keep-alive traffic.

How to spot stale presence early

First sign is inconsistency across sessions. One user logs in from their laptop, then opens the mobile app - and both devices race to update the same presence record. The status flickers between online and offline as each connection writes its last-seen timestamp.

Second sign is ghost presence after a hard disconnect. A user closes their laptop mid-call - the connection drops, but the server never receives a clean goodbye. The presence record stays green for minutes until your timeout finally fires.

Spotting these early:

  • Track concurrent sessions per user, not one global status
  • Log when multiple connections write conflicting timestamps within seconds
  • Monitor for presence records that stay active beyond your expected connection lifetime

If you see any of these patterns in production, your presence logic is treating connections as interchangeable when they are not. Start with a free prototype that models sessions explicitly from the beginning.

A cleaner approach from the start

The right time to solve presence is when you first wire up WebSockets - before you have users relying on stale indicators.

Start by tracking connection state, not user state. When a socket opens, the user is online. When it closes, start a grace period before marking them offline. Grace period. That is your buffer against spurious disconnects.

The key is treating reconnects as continuity, not new sessions. If a user reconnects within the grace window, keep their status unchanged. If the grace period expires without a reconnect, mark them offline and log it as a stale connection.

This approach survives:

  • Brief network hiccups that resolve in seconds
  • Mobile background transitions that drop and restore the socket
  • Server restarts where clients reconnect faster than your timeout

You will still see edge cases - a user who disconnects and immediately closes their laptop - but those resolve themselves when the grace period expires. The system degrades predictably instead of guessing.