Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deekepMaks's avatar

Realtime user online/offline status

I wanted to get the user's online/offline status in real time. It turns out that I can find out when the user entered the site, but I can’t find out exactly when he left the site and disconnected his channel On one of the sites I found a solution:

Echo.join('channel').leaving(user => /* send a request to the server with a user status change */)

But as far as I know, the leaving event fires for other users when the current user leaves. Therefore, there are two problems:

  1. The request to update the user's status will go multiple times from different clients when they know that someone has disconnected Of course, you can somehow prevent this by using a lot of checks, but I find it inconvenient
  2. If there is no one online, then the leaving event will not work at all and the user will remain online

In socket.io, for example, you can use the socket.on('disconnect', () => {}) event on the server side, which fires when one of the sockets is disconnected Is there any analogue in broadcast to know about user disconnect Or is there some kind of event similar to leaving that fires when the site tab is closed and sends a request to the server from the client side, which is disconnected?

0 likes
10 replies
Jsanwo64's avatar

There are a few ways you could approach detecting user status in real-time with Broadcast:

Use the presence state to track which users are currently connected. When a user joins a channel, you can update their presence to online. And when they leave or disconnect, you can update it to offline. This gives you a real-time view of who is currently active. Use the Echo.join callback to send an API request when a user joins a channel, setting their status to online. And use the Echo.leave callback to send a request when they leave, setting them to offline. This avoids multiple requests on leave. Send a periodic "heartbeat" event from the client that updates a "last seen" timestamp for the user. If you don't receive the event after a certain timeout, you can assume the user disconnected. On the server, track individual channel connections and send disconnect events when a connection is closed for a specific user. This avoids relying on presence or client events. So in summary, presence, join/leave callbacks, heartbeats, and server-side disconnection events are some ways to detect real-time user status with Broadcast. Using a combination of these approaches is likely the most robust.

Jsanwo64's avatar

@Snapey Yea. Needed to get him an answer faster. That would make him rethink and reimplement his logic flow.

deekepMaks's avatar

I decided that the best and easiest way would be to use the onload and beforeunload events to send navigator.sendBeacon() when the user opens / closes the site page This will set the online status when the user enters the site and remove the online status when the user logs out.

Snapey's avatar

@deekepMaks why do you need websockets, a simple xhr post request back to the server would do it easily.

However, the events you mentioned only work in the context of a single window so are an unreliable indicator of 'online' status ( ie, user has multiple tabs open)

deekepMaks's avatar

@Snapey RE: why do you need websockets, a simple xhr post request back to the server would do it easily.

i need accurate data

Snapey's avatar

@deekepMaks what does that mean? you don't think xhr/ajax works as a concept? Or you don't trust your implementation?

deekepMaks's avatar

@Snapey I need accurate online status data with an accuracy of a few seconds. When you talk about xhr/ajax, I think of it as a task that runs every few minutes using setTimeuot(), in which case the data will not be entirely accurate

The problem with several tabs is solved quite simply: with each onload event, a request will be sent to the server, there I can also define some counter that counts the number of such requests When disconnecting from the page, a request is also made to the server, where this counter is decremented. If the counter has decreased to 0, the user has closed all his tabs and then his status can be determined as offline

Snapey's avatar

I think of it as a task that runs every few minutes using setTimeuot()

no, that's not what I meant. I was referring to the events you already mentioned, eg when the user is navigating away from your site, THEN send an xhr message

deekepMaks's avatar

@Snapey I don't see any problem using navigator.sendBeacon. Judging by the documentation, it is designed just for such actions.

Please or to participate in this conversation.