Search code examples
phplaravelwssfsockopenlaravel-websockets

PHP Websocket Client


I'm currently attempting to create a WebSocket client to connect to a wss:// URL with PHP (without using JavaScript). It's been a few days trying to program a simple function or class that can do so.

I tried and looked into fsockopen, various streams, and also Racket but failed to see in their docs a client class. Is this possible with PHP? Code sample or any direction would be appreciated.


Solution

  • For anyone looking in the future this is what I found and used. Walk in the park.

    PHP-WSS

    <?php
    use WSSC\WebSocketClient;
    use \WSSC\Components\ClientConfig;
    
    $client = new WebSocketClient('ws://localhost:8000/notifications/messanger/yourtoken123', new ClientConfig());
    $client->send('{"user_id" : 123}');
    echo $client->receive();