Definition

What Is User Presence? How Online Status and Last Seen Work

The green dot looks simple. Behind it is a live connection, a heartbeat, a grace period and a fan-out problem that grows with every user.

Live presence vs last seen

Live presence

Last seen

What it shows

Online, offline, away or busy right now

When the user was last active

Where it comes from

An open connection and its heartbeats

A timestamp saved when the user goes offline

How it updates

Pushed to subscribers the moment it changes

Read when a profile or chat opens

Server cost

High; every change fans out to subscribers

Low; one write per session

Privacy

Shows availability

Shows habits over time, so many apps let users hide it

Best for

Deciding whether to message or call now

Setting expectations about a reply

Common bug

Ghost "online" after a silent disconnect

Wrong times from device clocks; use server time

What is user presence?

Presence is the green dot on an avatar, the "Active now" in a chat header, and the "last seen 10 minutes ago" under a name. It tells people whether it's worth messaging or calling now, or whether to expect a slow reply.

Most apps combine a few states:

  • Online and offline: worked out by the server from the user's connection.

  • Away or idle: set by the client after a period with no input.

  • Busy or do not disturb: set by the user, often with a custom status message.

  • Last seen: a timestamp stored when the user goes offline.

Presence is old. XMPP, the protocol behind many early chat apps, standardized presence subscriptions so you only see the status of people who allowed it.

How presence works

1. Connect

When the app opens, it opens a persistent connection, usually a WebSocket, and authenticates. The presence service marks the user online and notes the time.

2. Heartbeat

Connections often die without a goodbye. A phone goes into a tunnel, a laptop sleeps, a router drops an idle connection. Neither side gets a close message. So the server and client exchange small heartbeats every few seconds, using WebSocket ping and pong frames or an app-level ping. Several missed heartbeats mean the connection is gone.

3. Grace period, then offline

Mobile networks drop and reconnect all the time. Waiting a short grace period before marking someone offline means a quick reconnect causes no flicker. If they don't come back, the server marks them offline and saves the last-seen time.

4. Fan-out

The change goes to everyone subscribed to that user: contacts, people with the chat open, group members. This is where presence gets expensive.

Scaling presence fan-out

One user with 500 contacts going online creates 500 events. In a 10,000-member community where everyone logs on at 9 a.m., the events multiply fast, and most of them update screens nobody is looking at. Ways to keep it under control:

  • Subscribe narrowly: only to friends, specific roles, or the users visible on screen.

  • Fetch, don't push, for long lists: load presence for the rows a user can see when they render.

  • Debounce: collapse online, offline, online within a few seconds into no change.

  • Expire, don't trust: keep presence in a fast store with a time-to-live that heartbeats refresh, so a crashed server can't leave ghost "online" users.

  • Summarize big rooms: show "1,240 online" instead of a dot for each member.

Rule of thumb: never let presence depend on a disconnect event arriving. Expire it unless heartbeats keep renewing it.

Example: heartbeat and grace period in Node.js

This server uses the ws library's ping and pong to find dead sockets, then waits 30 seconds before marking the user offline.

const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({ port: 8080 });
const GRACE_MS = 30_000;
const offlineTimers = new Map();

wss.on('connection', (ws, req) => {
  const userId = authenticate(req);            // your auth
  clearTimeout(offlineTimers.get(userId));     // came back in time: no flicker
  setPresence(userId, 'online');
  ws.isAlive = true;
  ws.on('pong', () => { ws.isAlive = true; });
  ws.on('close', () => {
    offlineTimers.set(userId, setTimeout(() => {
      setPresence(userId, 'offline', Date.now()); // save last seen, notify subscribers
    }, GRACE_MS));
  });
});

setInterval(() => {                            // every 15 s
  wss.clients.forEach((ws) => {
    if (!ws.isAlive) return ws.terminate();    // missed a pong: fires 'close'
    ws.isAlive = false;
    ws.ping();
  });
}, 15_000);

This tracks one connection per user. Real apps count connections per user across devices and go offline only when the count reaches zero.

Where presence matters

  • Chat apps: contact lists and chat headers show who is around before you message.

  • Voice and video calling: show the call button prominently when the other person is online, or warn that they may not pick up.

  • Customer support: route new chats only to agents who are online.

  • Telehealth: show patients when their provider is available for a visit.

  • Gaming and communities: see which friends are online and invite them to a match.

  • Notifications: send a push notification when the user is offline, and skip it when they're active in the app.

Limits and pitfalls

  • Ghost online. Without heartbeats and expiry, a user who lost signal stays online for hours.

  • Flicker. No grace period means a dot that blinks on and off on every network change.

  • Multiple devices. A user is online if any device is connected. Track connections, not a single flag.

  • Background apps. Mobile operating systems suspend background apps, so the socket closes even though the user is reachable through push.

  • Privacy. Last seen reveals sleep and work patterns. Let users hide it.

  • Clocks. Use server time for last seen. Device clocks drift.

User presence in CometChat

If you're building chat or calling into your app, CometChat tracks the connections and sends presence changes for you.

Online, offline and last seen, scoped to who should see it

You choose whose presence each user gets when you initialize the SDK: subscribePresenceForAllUsers(), subscribePresenceForRoles() or subscribePresenceForFriends(). If you pick none, no presence events are sent. A UserListener receives onUserOnline and onUserOffline, and every User object carries a status (online or offline) and lastActiveAt for "last seen". The SDK's connection to CometChat runs over WebSockets and reconnects on its own after a drop, with a ConnectionListener to show connection state. The UI Kits display presence in conversation lists, message headers, user lists and group member lists.

Read the presence docs · Start building free

Frequently asked questions

What does "online" mean in a chat app?

It means the app on at least one of the user's devices has a live connection to the server right now. It doesn't guarantee they're looking at the screen.

How do apps know when you go offline?

Your app sends regular heartbeats over its connection. When the server stops hearing them, or the connection closes, it waits a short grace period and then marks you offline.

What is the difference between online and last seen?

Online is a live status that updates in real time. Last seen is a timestamp saved when you went offline, shown when you are not online.

Why does someone show as online when they're not using the app?

Their app may be open in the background, open on another device, or their connection dropped without the server noticing yet. Good presence systems use heartbeats and expiry to clear these quickly.

How do you build an online status indicator?

Keep a WebSocket open per user, exchange heartbeats, mark users offline after a missed-heartbeat grace period, store last seen, and publish changes only to the users who should see them.

Related terms