Skip to content
Advertisement

PHP Websocket Client – Keep connection open

I am using PHP-WSS in a laravel application and need to keep a websocket client open to receive various messages from the websocket server.

So far I built a CLI php script that I can execute and wait for messages to arrive.

I built the following function to test…

The question is, to keep the connection open for any messages that might be sent from the server, is it a good approach to do it as below using a while(true) loop? Is there anyway I can do this better? (To me it looks dirty and wish to improve it and do it properly)

JavaScript

UPDATE: Anyone using PHP-WSS I found a bug in Connection.php in broadCast method.

Original function tries to send on a dead connection, which shows the following error Empty read; connection dead? (Note the EOF = true)

JavaScript

I changed it to

JavaScript

Advertisement

Answer

Since the php script that renders the page finishes execution, you need to implement websockets on the client itself using a js script

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement