Make socket.io sharing session with express automatically
At the moment we have to use this code here to share session between socket.io and express.
//------------------------------------------------------------------------------
// WebSocket and Express session sharing through handshake
//------------------------------------------------------------------------------
var parseCookie = connect.utils.parseCookie;
io.set('authorization', function (data, accept) {
if (data.headers.cookie) {
// Whatever you put in the data object will be available in socket.handshake later on in connection event handler
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];
accept(null, true);
} else {
accept('No cookie transmitted.', false);
}
});
Can't this be done automatically by socket.io so the users don't have to reinvent the wheel every time.
Not everyone knows how to do it correctly.
+1 for this. At least it can be written in the docs on how to do it
+1 for this
parseCookie doesn't exist any more.
Use cookieParser see http://www.senchalabs.org/connect/cookieParser.html
More details https://github.com/senchalabs/connect/issues/600
Thanks John. But look, the point of this thread is to Make socket.io sharing session with express automatically
At the moment it doesn't. This discussion is not about the parserCookie / cookieParser issue but about session handling in general.
Leaving broken code on the web doesn't help anyone, and I can read, thanks.
You're right about leaving broken code on the web :+1:
Just want to make sure this thread doesn't go off-topic ...
Here is a module that does that https://github.com/functioncallback/session.socket.io
@gtramontina - Apparently that one isn't fully maintained:
https://github.com/wcamarao/session.socket.io/issues/40
Why is this closed if doesn't work?
Because as of Socket1 auth is handled completely by middleware so is offloaded from socket io. It's a bit like Microsoft Word removing functionality to change font size by palming it off to Microsoft Windows.