AmP
AmP

Reputation: 61

How to handle client-sessions in socket.io

I use client-sessions, not the express sessions. How could I get session data. Session stores on the client, not on a server. I use client-session module https://github.com/mozilla/node-client-sessions

Upvotes: 3

Views: 874

Answers (2)

AmP
AmP

Reputation: 61

I found the right answer, to get session from cookie, first you should parse the cookie

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

Than you have to decode the cookie, I used the original function from 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 holds your session data

Upvotes: 2

user3054852
user3054852

Reputation: 189

If you want the session data on the client, you could just use the module's built-in features. If you need it on the server, then you could get the information on client-side and then emit it with socket.io, something like socket.emit('sendSocketData', dataToSend);

Upvotes: 0

Related Questions