PHP connect as client to NodeJS Web socket

117 views Asked by At

Need my laravel app to connect to a nodejs based socket server. The function needs to pass custom headers in order to authenticate as well as keep pinging the connection every 1 minute in order to prevent disconnection from the socket server

Can anyone recommend any good libraries to use to achieve above functionality. Here, the PHP is acting as a client

Thanks in advance

1

There are 1 answers

0
maximus 69 On

Thanks for you suggestions, I ended up using ratchet/pawl client, which works well:

Example:

$headers = ['HEADER' => 'Value'];
$ip = '1.1.1.1';

\Ratchet\Client\connect($ip,[],$headers)->then(function($conn) use ($payload) {
  $conn->on('message', function($msg) use ($conn) {
      $response = json_decode($msg, TRUE);
      var_dump($response);
      $conn->close();
  });

  $conn->send('Hello!');

  $conn->on('close', function ($code = null, $reason = null) use ($connector, $loop, $app) {
    echo "Connection closed ({$code} - {$reason})\n";
  });
}, function ($e) {
    echo "Could not connect: {$e->getMessage()}\n";
});