On NPM: session.socket.io-express4
ADDED FIX FOR EXPRESS 4.X (eiriklv)
This tiny node module aims to simplify your socket.io application when using http sessions from express or connect middlewares. It has no dependencies and can be initialized using any session store and cookie parser compatible with express or connect.
It's written and tested using express 4.4.1 and socket.io 0.9.17.
Import the module and initialize it providing the required parameters
var SessionSockets =sessionSockets = io sessionStore ;
Listen to socket connections and get the socket as provided by socket.io, together with either the session or an error
$ cd example $ npm install $ node server.js Visit http://localhost:3000
Note that now you receive 3 parameters in the connection callback (err, socket, session). The first will be an error object (if an error has occured) from either the cookie parser (when trying to parse the cookie) or the session store (when trying to lookup the session by key); the second will always be the socket as provided by socket.io; and the third (if no error has ocurred) will be the corresponding user session for that socket connection.
The cookieParser doesn't need to be the same reference, you can create another instance somewhere else, but it should take the same 'secret', otherwise the cookie id won't be decoded, therefore the session data won't be retrieved.
The sessionStore must be the same instance. It's quite obvious why.
You can always debug the cookies and session data from any socket.handshake. The socket is the same as provided by socket.io and contains all of that information.
When looking up for the cookie in a socket.handshake, SessionSockets will take precedence on the following order:
You can specify your own session store key
io sessionStore cookieParser 'yourOwnSessionStoreKey';
It defaults to 'connect.sid' (which is default for both connect and express).
var http =connect =express =app = ;
Below are the two main references you will need to keep
var cookieParser = expresssessionStore = session;
Both will be used by express - so far everything's familiar, except that you need to provide sessionStore when using express.session(). Here you could use Redis or any other store as well.
Then you create the server and bind socket.io to it (nothing new here)
var server = httpio = ;
Now instead of listening to io.sockets.on('connection', ...) you will pass it together with the sessionStore and cookieParser
var SessionSockets =sessionSockets = io sessionStore cookieParser;
Which will use it all together to get you what matters
(The MIT License)
Copyright (c) 2012 Wagner Camarao <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.