1
votes

Is it possible to broadcast or emit data over to browser and catch it with javascript to treat it accordingly, using ReactPHP without Ratchet?

I have a periodicTimer on event-loop and sometimes it needs to broadcast or emit to users connected by a socket. I saw there was a class socket and in the examples on the React github project page it was instantiated but not used in web-flow, only on netcat. Is it possible to make it work natively on browsers like socket.IO or Ratchet?

The factory and connection works fine, I just don't know how to bind socket to send/receive data to the server with event-loop.

1

1 Answers

2
votes

Broadcasting this is not super-special function.

First of all, you need save all user connection in global scope. For example you have a class TestServer, it can contains attribute connections (type array). In method run you write loop.

For example this your connection event

$server->on('connection', function(ConnectionInterface $conn) {
     $this->connections[] = $conn;
});

And write method

public function broadcast($message) {
     foreach($this->connections as $conn) {
          $conn->write($message);
     }
}

Attention! You must write code to delete disconnecting if user disconnect, and if you need, you can write logic about topics.