Swoolle with RabbitMQ

I am trying to send some data from a php application to the user's browser using websockets. So I decided to use Swoole in combination with RabbitMQ.

This is the first time I have been working with websockets and after reading some posts about Socket.IO, Ratchet, etc. I decided to stop Swoolle because it is written in C and is convenient for use with php.

This is how I understood the idea of ​​enabling data transfer using websockets: 1) Launch the RabbitMQ worker and Swoolle server in the CLI 2) The php application sends data to RabbitMQ 3) RabbitMQ sends a data message to the worker 4) The worker receives a data message + establishes a socket connection with Swool socket server. 5) Swoolle server transfers data to all connections

The question is how to connect Swoolle socket server with RabbitMQ? Or how to get RabbitMQ to establish a connection with Swoolle and send data to it?

Here is the code:

Swoolle server (swoole_sever.php)

$server = new \swoole_websocket_server("0.0.0.0", 2345, SWOOLE_BASE); $server->on('open', function(\Swoole\Websocket\Server $server, $req) { echo "connection open: {$req->fd}\n"; }); $server->on('message', function($server, \Swoole\Websocket\Frame $frame) { echo "received message: {$frame->data}\n"; $server->push($frame->fd, json_encode(["hello", "world"])); }); $server->on('close', function($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start(); 

A worker who receives a message from RabbitMQ then connects to Swoolle and passes the message through a socket connection (worker.php)

 $connection = new AMQPStreamConnection('0.0.0.0', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('task_queue', false, true, false, false); echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; $callback = function($msg){ echo " [x] Received ", $msg->body, "\n"; sleep(substr_count($msg->body, '.')); echo " [x] Done", "\n"; $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); // Here I'm trying to make connection to Swoole server and sernd data $cli = new \swoole_http_client('0.0.0.0', 2345); $cli->on('message', function ($_cli, $frame) { var_dump($frame); }); $cli->upgrade('/', function($cli) { $cli->push('This is the message to send to Swoole server'); $cli->close(); }); }; $channel->basic_qos(null, 1, null); $channel->basic_consume('task_queue', '', false, false, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); 

A new task in which a message will be sent to RabbitMQ (new_task.php):

 $connection = new AMQPStreamConnection('0.0.0.0', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('task_queue', false, true, false, false); $data = implode(' ', array_slice($argv, 1)); if(empty($data)) $data = "Hello World!"; $msg = new AMQPMessage($data, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT) ); $channel->basic_publish($msg, '', 'task_queue'); echo " [x] Sent ", $data, "\n"; $channel->close(); $connection->close(); 

After starting both swool servers and workers, I start new_task.php from the command line:

 php new_task.php 

On the command line where RabbitMQ Worker (worker.php) works, I see that the message is being delivered to the employee (the message “[x] Received Hello World!” Appears).

However, nothing happens on the command line where the Swoolle server is running.

So the questions are: 1) Is the idea of ​​this approach right? 2) What am I doing wrong?

+7
php websocket sockets rabbitmq swoole
source share

No one has answered this question yet.

See related questions:

647
ActiveMQ or RabbitMQ or ZeroMQ or
74
How to send and receive WebSocket messages on the server side?
12
New RabbitMQ connection refused due to SocketException
3
rabbitmq AlreadyClosedException
2
RabbitMQ with Websocket and Gevent
0
Msgpack error when using rabbitmq and django channels
0
RabbitMQ multiple consumers with one connection object
0
How to send and receive data from webstomp websocket provided by rabbitmq?
0
Swoolle sends websocket data from server to client

All Articles