How to track users' data when using web sockets and Ratchet?

Posted In PHP
  • lincon-lincon 2 months ago
    Hello everyone,,
    I'm starting to use Ratchet for my first-ever web sockets implementation. I'm converting from Ajax. In my Ajax model, I store the user's username, plus [https://showbox.bio](https://showbox.bio/) a bunch of variables being used across the application, in the session. With sockets, the socket server is a single instance running on its own, so can't use the session global. How do I track users, given [https://tutuapp.uno/](https://tutuapp.uno/) that the $connection->resourceId [https://vidmate.vet/](https://vidmate.vet/) variable Ratchet offers can't be trusted to refer to the same user over the lifetime of the client's use of my website?
    
    So far, my only idea is to generate a long string of random letters and numbers in my onOpen() function, and send that to the client, which will save it. Any message from the client will then include the "key" the server gave it, thus identifying which client it is. If the client receives a new key, when a connection drops and is re-established, the new key is ignored and the previous one used instead. That should let me keep the user's data associated with the right user.
    
    Is there a better way to do this? Is there anything about all this that I'm overlooking? Thanks for any ideas.
    

    Hello everyone,, I'm starting to use Ratchet for my first-ever web sockets implementation. I'm converting from Ajax. In my Ajax model, I store the user's username, plus https://showbox.bio a bunch of variables being used across the application, in the session. With sockets, the socket server is a single instance running on its own, so can't use the session global. How do I track users, given https://tutuapp.uno/ that the $connection->resourceId https://vidmate.vet/ variable Ratchet offers can't be trusted to refer to the same user over the lifetime of the client's use of my website?

    So far, my only idea is to generate a long string of random letters and numbers in my onOpen() function, and send that to the client, which will save it. Any message from the client will then include the "key" the server gave it, thus identifying which client it is. If the client receives a new key, when a connection drops and is re-established, the new key is ignored and the previous one used instead. That should let me keep the user's data associated with the right user.

    Is there a better way to do this? Is there anything about all this that I'm overlooking? Thanks for any ideas.

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
tnylea
Cheers Friend!

I'm Tony, the creator of this site. I'm trying to increase my twitter following. Want to follow me?

Follow @tnylea If not, no worries :)