title: Swoole with RabbitMQ
body: <p>I'm trying to send some data from php application to the user's browser using websockets. Therefore I've decided to use <a href="https://www.swoole.co.uk/docs/modules/swoole-websocket-server" rel="noreferrer">Swoole</a> in combination with RabbitMQ.</p>
  
  <p>It's the first time I'm working with websockets and after reading some posts about Socket.IO, Ratchet, etc. I've decided to halt on Swoole because it's written in C and handy to use with php.</p>
  
  <p>This is how I understood the idea of enabling data transfer using websockets: 
  1) Start RabbitMQ worker and Swoole server in CLI
  2) php application sends data to RabbitMQ
  3) RabbitMQ sends message with data to worker
  4) Worker receives message with data + establishes socket connection with Swoole socket server.
  5) Swoole server broadcasts data to all connections</p>
  
  <p>The question is how to bind Swoole socket server with RabbitMQ? Or how to make RabbitMQ to establish connection with Swoole and send data to it?</p>
  
  <p>Here is the code:</p>
  
  <p>Swoole server (swoole_sever.php)</p>
  
  <pre><code>$server = new \swoole_websocket_server("0.0.0.0", 2345, SWOOLE_BASE);
  
  $server-&gt;on('open', function(\Swoole\Websocket\Server $server, $req)
  {
      echo "connection open: {$req-&gt;fd}\n";
  });
  
  $server-&gt;on('message', function($server, \Swoole\Websocket\Frame $frame)
  {
      echo "received message: {$frame-&gt;data}\n";
      $server-&gt;push($frame-&gt;fd, json_encode(["hello", "world"]));
  });
  
  $server-&gt;on('close', function($server, $fd)
  {
      echo "connection close: {$fd}\n";
  });
  
  $server-&gt;start();
  </code></pre>
  
  <p>Worker which receives message from RabbitMQ, then makes connection to Swoole and broadcasts the message via socket connection (worker.php)</p>
  
  <pre><code>$connection = new AMQPStreamConnection('0.0.0.0', 5672, 'guest', 'guest');
  $channel = $connection-&gt;channel();
  
  $channel-&gt;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-&gt;body, "\n";
      sleep(substr_count($msg-&gt;body, '.'));
      echo " [x] Done", "\n";
      $msg-&gt;delivery_info['channel']-&gt;basic_ack($msg-&gt;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-&gt;on('message', function ($_cli, $frame) {
          var_dump($frame);
      });
  
      $cli-&gt;upgrade('/', function($cli)
      {
          $cli-&gt;push('This is the message to send to Swoole server');
          $cli-&gt;close();
      });
  };
  
  $channel-&gt;basic_qos(null, 1, null);
  $channel-&gt;basic_consume('task_queue', '', false, false, false, false, $callback);
  
  while(count($channel-&gt;callbacks)) {
      $channel-&gt;wait();
  }
  
  $channel-&gt;close();
  $connection-&gt;close();
  </code></pre>
  
  <p>New task where the message will be send to RabbitMQ (new_task.php):</p>
  
  <pre><code>$connection = new AMQPStreamConnection('0.0.0.0', 5672, 'guest', 'guest');
  $channel = $connection-&gt;channel();
  
  $channel-&gt;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' =&gt; AMQPMessage::DELIVERY_MODE_PERSISTENT)
  );
  
  $channel-&gt;basic_publish($msg, '', 'task_queue');
  
  echo " [x] Sent ", $data, "\n";
  
  $channel-&gt;close();
  $connection-&gt;close();
  </code></pre>
  
  <p>After starting both swoole server and worker I'm triggering new_task.php from command line:</p>
  
  <pre><code>php new_task.php
  </code></pre>
  
  <p>In command line prompt where a RabbitMQ Worker is running (worker.php) I can see that a message is delivered to the worker ("[x] Received Hello World!" message is appearing). </p>
  
  <p>However in command line prompt where Swoole server is running happens nothing.</p>
  
  <p>So the questions are:
  1) Is the idea of this approach right?
  2) What am I doing wrong?</p>