Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access extra parameters in Ratchet web socket requests

I've set up Ratchet for websockets in PHP . It is connecting fine from my javascript client using (ws://localhost:8080) and successfully send/receive messages. But I want to pass some params like (ws://localhost:8080?param1=value). I'm not able to figure out how can I access param1 in my PHP script.

If possible in MessageComponentInterface::onOpen(ConnectionInterface $conn) method.

Or better: Can I associate those params with ConnectionInterface $conn. So that I've them for further communication.

I've followed http://socketo.me/docs/hello-world.

like image 441
Piyuesh Avatar asked Mar 31 '14 12:03

Piyuesh


3 Answers

In symfony 4 with php > 7.1

$conn->httpRequest->getUri()->getQuery()

This return all parameters into the query, only need parse string to extract the paremeter that needs.

like image 158
vgpastor Avatar answered Oct 20 '22 21:10

vgpastor


As of a very recent update, you can now access this like so:

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

I actually just ran into this issue myself. Tested this and it's working perfectly.

like image 16
CJ Thompson Avatar answered Oct 20 '22 23:10

CJ Thompson


$conn->WebSocket->request 

replaced with

$conn->httpRequest 

which is a PSR-7 object

https://github.com/ratchetphp/Ratchet/blob/master/CHANGELOG.md

like image 4
Stanislav Avatar answered Oct 20 '22 22:10

Stanislav