zmq icon indicating copy to clipboard operation
zmq copied to clipboard

Could you provide a websocket pub/sub example?

Open dschissler opened this issue 5 years ago • 3 comments

I'm converting a tech demonstration from react/ratchet to swoole. I've only been working with Swoole for a total of 15 minutes now and I don't know where to start.

For example this is a react/ratchet websocket server that I'm adapting.

        $opts = $params['opts'];
        $config = $this->config;

        $wsPort = (isset($opts['wsport'])) ? $opts['wsport'] : $config->app->wsPort;
        $zmqPort = (isset($opts['zmqport'])) ? $opts['zmqport'] : $config->app->zmqPort;

        $loop = EventLoopFactory::create();

        $broadcaster = new Broadcaster();
        $broadcaster->setDI($this->getDI());

        // Listen for the web server to make a ZeroMQ push after an ajax request
        $context = new ReactZMQContext($loop);
        $sub = $context->getSocket(ZMQ::SOCKET_SUB);
        $sub->subscribe('');
        $sub->bind("tcp://127.0.0.1:$zmqPort");
        $sub->on('message', [$broadcaster, 'onPost']);

        $wsServer = new WsServer($broadcaster);
        $httpServer = new HttpServer($wsServer);
        $socket = new Reactor("0.0.0.0:$wsPort", $loop);
        $ioServer = new IoServer($httpServer, $socket, $loop);

I think that something a simplified version of this would be a good addition to your examples.

dschissler avatar Apr 21 '19 00:04 dschissler

The idea is to take ajax requests from anywhere (or pushed from anywhere) and send a message via the websocket. I like having tech like this worked out so that I'm ready to go with it when something comes up.

dschissler avatar Apr 21 '19 00:04 dschissler

So msg/event comes in over AJAX or ZMQ and you want to push these out over websocket? Is that what you're looking for?

ClosetGeek-Git avatar May 03 '19 18:05 ClosetGeek-Git

Hi,

I've just found this thread and i'm attempting to do the same thing here. As in i would like to add a ZMQ pull socket to the swoole websocket server. The idea here is that independent workers would signal the swoole server via zmq which in turn would send a response to the frontend via websocket.

My problem is that i do not know how to add the zmq listener. As a standalone service it would be in a while loop, but swoole already have an event loop running.

Thanks

qury avatar Jun 06 '21 20:06 qury