You output the socket id, not the session id from express.js.
You must use the authorization event, its first parameter is an object that has an entry called sessionID . This value should not change between page reloads, as it is stored in a cookie (or in the redis database or any other).
Here is a good article explaining how this works: http://www.danielbaulig.de/socket-ioexpress/ , but it's a bit outdated. The basic principle remains the same, but several details have changed. For example, the server creation method no longer works, and connection developers removed parseCookie() . Users are not happy with this solution, however, this easy-to-remember line of code is a workaround:
connect.utils.parseSignedCookies(cookie.parse(decodeURIComponent(data.headers.cookie)), secret);
As I said, the article mentioned above should give you all the basics you need, if you want to see a working implementation, look at this: https://github.com/vortec/lolbr/blob/master/lib/lolbr.js
Inside the authorization event handler, you can modify the data object and get it later using socket.handshake , in your case: socket.handshake.sessionID .
Hope this helps.
Fabian
source share