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

Andi1982's avatar

Websockets best practices

Hello,

I did already few projects with laravel in the future. But just simple with blade-Views and jquery javascript-library for the frontend functionality.

But now I plan something new and would like to use websockets. I already understood the concepts with the server application like nodejs and the javascript client. But my problem is to understand how is the best practice, that the server-part gets the informations fast enough to spread the informations soon to all connected clients.

Just as example: Website wants to show ta current number of registered users and the current likes of a picture, without user has to reload all the page. For that I would want to use websockets.

So any user clicks the like button on the website, I could send this click event to websocket-server and he could update the counter in database spread fast the new number of likes to other clients, that's easy.

But if a user registers, how does the websocket-server notice fast enough the new registrations? Do I have to connect the RegisterController::storeUser() Function as a client to the websocket server to send events? Or has the websocket-server to listen on the database with "Select count() from users;" to get the new numbers?

I really would like to know, how is the best practise for that kind of jobs ...

Best regards Andi

0 likes
5 replies
Sinnbeck's avatar

I use websockets for chat and online presense, and it is VERY fast. Lets say a person sends a chat message. It gets sent to laravel as a post request which puts it in the queue. The job on the queue, then sends it to my websocket server (I use soketi but you can use pusher.com if you dont want to run your own), which sends it to the client browser. This is very very fast and the message ends up at the recipient in less than a second. And if it is send to multiple users, it is still just as fast.

Andi1982's avatar

Hey @Sinnbeck, you said that

sent to laravel as a post request which puts it in the queue

How does this queuing works exactly? so a controller-method puts it to the websocket-server or what kind of queue it is?

OussamaMater's avatar

@andi1982 It's the same concept for the "like" post you talked about, you see when a user register you can dispatch an event (Laravel already comes with a pre-defined event Illuminate\Auth\Events\Registered ) to which you may set a listener that broadcasts an event to the websockets server, I mean in the "like" post you mentioned you either used "notifications" or "events", you can do the same to when a user is registered, dispatch the event to the websockets server, and it will broadcast it to all the subscribed users for that specific channel.

Andi1982's avatar

Thanks guys, so I will have a deeper look at broadcasting from laravel.

Please or to participate in this conversation.