1

I have a problem with sending messages from ZeroMq to Ratchet-PHP. I've switched from Windows to Linux Centos 7.When I developed on Windows my code worked. When I check my php-zmq version with php --ri zmq:

zmq

ZMQ extension => enabled
ZMQ extension version => 1.1.3
libzmq version => 4.1.4

This is how I start Ratchet and ZMQ

    $pdo = new \PDO(.....);
    $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

    $dbOptions = array(
        'db_table'      => .....,
        'db_id_col'     => .....,
        'db_data_col'   => .....,
        'db_time_col'   => .....,
        'lock_mode'     => 0,
    );

    $pdoProvider = new PdoSessionHandler($pdo, $dbOptions);

    $loop = Factory::create();
    $vid = new Chat($pdo);

    $context = new Context($loop);
    $pull = $context->getSocket(\ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555');
    $pull->on('message', array($vid, 'handleZmqMessage'));

    $session = new SessionProvider(
        new WsServer(
            $vid
        ),
        $pdoProvider
    );

    $server = new App(myIp, 9899, '0.0.0.0');
    $server->route('/{something}', $session, array('*'));
    $server->route('/{something}/{otherThing}', $session, array('*'));
    $server->run();`

This is my WebSocket

class Chat implements MessageComponentInterface
{
protected $clients;

public function __construct() {
    $this->clients    = [];
}

public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
   ............
}

public function onMessage(ConnectionInterface $from, $msg) {
    $myId = $from->Session->get('currentId');
    $msgContent = json_decode($msg, true);
    switch ( $msgContent['command'] ) {
     ......
}

public function onClose(ConnectionInterface $conn) {
    ........... 
}

public function onError(ConnectionInterface $conn, \Exception $e) {
    ..........
}


public function handleZmqMessage($msg)
{
    $parseMsg = json_decode($msg, true);
    $id = (int)$parseMsg['id'];

    switch ( $parseMsg['command'] ) {
       ....... 
    }
}
} 

And this is how I send messages with ZeroMQ from my code after `AJAX:

$context        = new \ZMQContext(1);
$socket         = $context->getSocket(\ZMQ::SOCKET_PUSH);
$socket->connect("tcp://127.0.0.1:5555"); 
$socket->send(json_encode($arr));

My PHP 7.3.9. When I start Ratchet and ZeroMQ it does't shows any errors. When use ZeroMQ from my code there is no errors again. I am new in Linux but I think the problem come from it. Again this code worked on Windows but on Linux

Davosss
  • 61
  • 1
  • 7
  • How about testing a minimalistic MCVE-code? Avoid any high-level tricks that may have artifacts, like **`$context = new Context( );`** declare a trivial php-PUSH-er ( sending a time, or a incremented counter ), declare a trivial php-PULL-er ( receiving any messages and putting 'em on console ). If that works, the problem starts to get isolated as having nothing to do with Linux / ZeroMQ, but with the higher-level tricks. If it fails to work over as plain transport-class connection as the **`tcp://localhost:5555`** problem depends on host-side cfg or ZeroMQ installation. Ok? – user3666197 Sep 12 '19 at 18:19

0 Answers0