Reputation: 712
I have the following scenario: I have a WebSocket running with Ratchet (PHP WebSocket) I use the onMessage() callback function to handle incomming data and respond accordingly. If I get the 'start-broadcast' message via the WebSocket I have to start a loop which will send out a broadcast message to all connected clients on the WebSocket every 0.2 sec. So I need to make a loop which can do this, but I can't put it into the onMessage() function, as this will block, and I won't be able to receive any more messages via the WS. If I get the 'stop-broadcast' message via the WebSocket I have to stop the broadcast loop.
So basically I need a way to start and stop this loop, and have this loop running parallel to the WebSocket loop so it doesn't block up.
Problems: The Socket->send() method I'm pretty sure is not thread-safe, so I need to make sure that the WS loop and my broadcast loop are not trying to send a message at the same time.
Possible approaches I have considered:
ReactPHP/Promise
Somehow use this to make an async function inside which I have a loop. I have no experience with Promise, and I don't know if it can do what I need.
Running a spearate PHP-CLI process, and use ZMQ for inter-process communication between the WS instance and the Broadcast loop.
With this I could send message back and forth from the websocket, and
I could send a message to start or stop the broadcast, also I could
send a message from the broadcast loop to the WS loop to send out a
message to the WS clients.
Using pthreads
Spawn a new Thread for the broadcast loop, this can be killed when I
want it to stop. I'm pretty sure I'll have to make sure the Socket is
only used by one thread at a time, so I'll have handle that somehow.
My question is, which approach should I take, and are there any examples or tutorials for the suggested approach?
Upvotes: 0
Views: 1348
Reputation: 1697
If I get you right:
Maybe I misunderstood the problem, but it's a bad idea, getting client requests start and stop brodcasting (what if everyone will send a "start command"?)
In general, I would recommend using ZMQ. This is the most scalable solution. (It is best to separate the services)
OR
If you want a pub/sub service. Then simply create a Timer and have a list of who to broadcast. Client send "subscribe", and receive messages. Good idea use Redis for storing data between processes (WebSocket - ZMQ)
You need read ZMQ PHP DOC, before using ZMQ and see reactphp/zmq lib
Upvotes: 1