0

I use Swoole as a WebSocket server. Once per second I need to broadcast a message to all connected WS clients.

Naive approach: I set a server timer $server->tick() prior to launching a server:

$this->server->tick(1000, function () {
    $message = 'hello';
    foreach ($this->server->connections as $fd) {
        $this->server->push($fd, $message);
    }
});

Got errors:

[2020-05-05 12:23:56 #21985.2] ERROR swServer_tcp_send (ERRNO 9009) can't send data to the connections in master process

What is the correct way to push WebSocket messages not from a Master, but from a Worker process?

Serge
  • 1,531
  • 2
  • 21
  • 44
  • [LINK](https://www.swoole.co.uk/docs/modules/swoole-server-sendMessage) "sendMessage is blocking In the worker process" – Simone Rossaini May 05 '20 at 14:07
  • @SimoneRossaini thanks. My case is not `sendMessage()` to WorkerProcess, but rather the `Swoole\WebSocket\Server->push()` – Serge May 05 '20 at 14:13
  • 1
    So possible [here](https://github.com/swoole/swoole-src/issues/2019)? – Simone Rossaini May 05 '20 at 14:18
  • @SimoneRossaini indeed! Thanks! I came up to the similar solution, but with added singularity check: second parameter of the `onWorkerStart(\Swoole\Websocket\Server $server, int $worker_id)` should be `0` so that only one timer is launched. – Serge May 05 '20 at 14:31

0 Answers0