Search code examples
javascriptphpwebsocketratchet

RatchetPHP no WebSocket property for new connections


I'm trying to access the query parameters for incoming connections in a Websocket server's onOpen function using Ratchet. Both the official documentation and other StackOverflow posts say that you can do this by accessing the WebSocket property of the ConnectionInterface object passed to the function:

public function onOpen(ConnectionInterface $conn) {
    $query = $conn->WebSocket->request->getQuery();
}

However, there's no WebSocket property for incoming connection objects. When I start this server and connect with a client, a notice is given, which results in a fatal error for calling a function on a null object:

PHP Notice: Undefined property: Ratchet\Server\IoConnection::$WebSocket

I'm using PHP 7.0 and I'm requiring the latest stable release in my composer.json:

"require": {
    "cboden/ratchet": "^0.3.6"
}

I'm connecting the client in a Chrome JS console with code copied directly from the Hello World documentation as well:

var conn = new WebSocket('ws://localhost:8080?foo=bar');
conn.onopen = function(e) {
    console.log("Connection established!");
};

If it's helpful, the only public properties I see for $conn are:

bufferSize
stream
readable
writable
closing
loop
buffer
listenerse
decor

Solution

  • I had the same issue and I found that :

     $querystring = $conn->httpRequest->getUri()->getQuery();
     parse_str($querystring,$queryarray);
    

    allows you to access the query parameters.