Scaling Websockets in PHP

I have to implement some kind of sub-system for a larger platform, able to handle realtime communications (such as chat, notifications, and a few other things). And this means: websockets. And, as the platform itself is implemented in PHP/Laravel, this means: websockets in PHP.

So I get back to Ratchet, and I've realized this cannot work alone: as you have a single thread running, getting connections and handling the messages one at a time, the whole system is going to collapse very fast. Nor it can be scaled horizontally simply adding more instances, as different clients will be connected to different servers and you would not be able to share messages (e.g. chatting) among them.

So, I dug deeper and I've managed to include yet another element in the pattern: pub/sub communications through Redis (using ReactPHP-Redis, which conveniently uses ReactPHP exactly as Ratchet and can executed on the same mainloop). The idea is: have many instances, subscribe all of them to a common Redis channel, open the websocket, as soon as a new message arrives publish it on the same Redis channel to reach all the instances, and let each of them broadcast the notice to the owned involved websocket clients.

Design may vary (e.g. managing Redis publishing directly into LiveServer) and some optimization is required (e.g. keep on the LiveServer instance messages which can be locally managed, and which do not involve other clients), but this is the essential core.