I have a problem with websockets. So, I have an Rpi that provides me some data through the socketIO client in a rather simple way. The following code shows how I can get this data:
<!DOCTYPE html> <html> <header> <title>SocketIO test</title> <script src="http://192.168.5.5:8000/socket.io/socket.io.js"></script> </header> <body> <script type="text/javascript"> var client = io.connect('http://192.168.5.5:8000'); client.on('connect', function() { console.log('connected'); }); client.on('raw', function(data){ console.log(data); }); client.on('state', function(data){ console.log(data); }); </script> </body>
However, what I need to implement is a bit more complicated. I need to use the Apache server to determine some data before it gets to the client side. The following figure shows what I'm trying to build:

To achieve my goal, I tried several WebSocket servers and client libraries for PHP until I found PHPws , which looks like the best solution for my scenario. So, I read the examples, I test them, and everything went well until I tried to connect to Rpi with the following code:
require_once("../vendor/autoload.php"); $loop = \React\EventLoop\Factory::create(); $logger = new \Zend\Log\Logger(); $writer = new Zend\Log\Writer\Stream("php://output"); $logger->addWriter($writer); $client = new \Devristo\Phpws\Client\WebSocket("ws://192.168.5.5:8000", $loop, $logger); $client->on("connect", function() use ($logger, $client){ $logger->notice("Or we can use the connect event!"); $client->send("Hello world!"); }); $client->on("raw", function($message) use ($client, $logger){ $logger->notice("Got message: ".$message->getData()); $client->close(); }); $client->open()->then(function() use($logger, $client){ $logger->notice("We can use a promise to determine when the socket has been connected!"); }); $loop->run();
I more or less accepted this example from Devristo github.
On the server side, program execution does not cause any errors or messages.
Is it possible to build what I want to build here with PHPws? If so, am I connecting to the Rpi server correctly with the PHPws code example shown?