socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Make socket.io sharing session with express automatically

Open ghost opened this issue 14 years ago • 10 comments

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.

ghost avatar Jun 06 '12 21:06 ghost

+1 for this. At least it can be written in the docs on how to do it

kanle485 avatar Nov 08 '12 11:11 kanle485

+1 for this

binarykitchen avatar Feb 16 '13 05:02 binarykitchen

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

JohnMcLear avatar Feb 17 '13 23:02 JohnMcLear

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.

binarykitchen avatar Feb 18 '13 00:02 binarykitchen

Leaving broken code on the web doesn't help anyone, and I can read, thanks.

JohnMcLear avatar Feb 18 '13 00:02 JohnMcLear

You're right about leaving broken code on the web :+1:

Just want to make sure this thread doesn't go off-topic ...

binarykitchen avatar Feb 18 '13 00:02 binarykitchen

Here is a module that does that https://github.com/functioncallback/session.socket.io

gtramontina avatar May 29 '13 21:05 gtramontina

@gtramontina - Apparently that one isn't fully maintained:

https://github.com/wcamarao/session.socket.io/issues/40

framerate avatar Jul 03 '14 01:07 framerate

Why is this closed if doesn't work?

totty90 avatar Nov 23 '14 13:11 totty90

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.

JohnMcLear avatar Nov 23 '14 14:11 JohnMcLear