How do you send data to the server onload using ratchet websockets?

☆樱花仙子☆ 提交于 2021-01-29 12:07:02


I have session variables stored on the client. (in this example, session var is "username" with value "Foo") I need the server to know that the new connection that just loaded in has the name "Foo." is there any way to have the server know the value of a session variable?


No, you cannot do that. Sessions are stored in the cookies and Ratchet cannot access them. However, I use this approach when I am working with web-sockets:

  1. Create a hash when the user logins into the account and store it in the database to identify the user with it.

  2. Connect to the Ratchet web-socket and then send an initialize message with the value of the hash (easy, just echo the hash into the JavaScript or load it through Ajax).

  3. If the hash is valid at the Ratchet end, good. Otherwise, disconnect the user.