r/laravel•
LogicCube

Laravel Echo - delay leaving a channel on page reload

I recently setup Laravel Echo and Broadcasting, first with pusher and then switched it to laravel-websockets. It is working very well and I can join channels, show online status, broadcadt messages from the backend etc. The only "problem" i have is that when a user reloads a page or changes a route, the user will be shown offline for a second or two because the connection is closed (leave) and then reconnected upon page load. It's not a big deal, but in the past I used a combination of socket.io (client) and nodejs/socket io and there was an option to enforce the usage of a single socket and it had the option to delay the leave/reconnect so there was no short "online/offline/online".

So my question is: does anyone have an idea if there is a built-in option in Echo to handle this? I could not find anything about this in the documentation. My current thought would be to add a delay or sth like this to the leave/join methods of the echo client, and maybe see if the user comes back within ~3 seconds so i would not update the array of currently connected users (using vue js by the way...)

I would be very happy to hear your thougths on this.

Posted Aug 8, 2019, 4:37 PM UTC

01
iShot_csgo•

I used to work on a project (and sometimes still do) where people can quiz against each other online, all through Laravel and Websockets). This project had the same problem you're describing right now.

I would show a notification when a user 'disconnected' / 'left' on each of the other connected client's. This was all done front-end over sockets so the only thing I had to do was debounce a function before checking whether the user really is online (with a 5 seconds delay for example).

1