Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a session within a ratchet websocket connection

We have built a Ratchet websocket server that serves a standalone client application. The server is running on port 8080, and our Symfony app is running on port 80. It's critical that we have sessions working within the websocket server so we can uniquely identify each client. The problem is that Ratchet does not send a set-cookie header over port 8080.

Hoping to find a way to send the set-cookie header in response to the upgrade request on 8080, I tried to start a new session in onOpen():

use Symfony\Component\HttpFoundation\Session\Session;

class ClientApi implements MessageComponentInterface {
    /**
     * @inheritDoc
     */
    public function onOpen(ConnectionInterface $conn) {
        $conn->Session = new Session();
        $conn->Session->start();
    }
    ...

But this did not result in a set-cookie header being sent back in the response to the upgrade request.

We have a workaround where the client has to first do a GET on port 80 to get a cookie before sending the websocket upgrade request over 8080, so it can send the cookie with the upgrade request. I'm hoping to get something working like I tried above, so the client does not have to bother the web app on port 80.

Am I missing something?

like image 955
mattexx Avatar asked Nov 02 '22 23:11

mattexx


1 Answers

I believe by default creating a new Symfony Session class will use global variables. Ratchet provides a Session middleware class that will attach a Symfony Session object to each ConnectionInterface.

like image 66
cboden Avatar answered Nov 15 '22 09:11

cboden