How to handle client sessions in socket.io

I use client sessions, not express sessions. How can I get session data. Sessions are stored on the client, not on the server. I use the client-session module https://github.com/mozilla/node-client-sessions

+3
source share
2 answers

I found the right answer to get a session from a cookie, you must first analyze the cookie

handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);

Than you should decode the cookie, I used the original function from the client-session module

var clientSessions = require('./node_modules/client-sessions/lib/client-sessions')

    var opts = { 
      cookieName: 'yourSessionName'
      , secret: 'secret'
    }
    var decoded = clientSessions.util.decode(opts, handshakeData.cookie['yourSessionName']) 

decoded object stores your session data

+2
source

, . , , socket.io, - socket.emit('sendSocketData', dataToSend);

0

All Articles