npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

session.socket.io-express4

v0.1.1

Published

Express middleware sessions in socket.io

Downloads

17

Readme

session.socket.io (SessionSockets)

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 3.0.0rc4, connect 2.4.5 and socket.io 0.9.10.

Quick Start

Import the module and initialize it providing the required parameters

var SessionSockets = require('session.socket.io')
  , sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

Listen to socket connections and get the socket as provided by socket.io, together with either the session or an error

sessionSockets.on('connection', function (err, socket, session) {
  //your regular socket.io code goes here
  //and you can still use your io object
});

Running the example

$ cd example
$ npm install
$ node server.js

Visit http://localhost:3000

Saving values into the session

sessionSockets.on('connection', function (err, socket, session) {
  session.foo = 'bar';
  //at this point the value is not yet saved into the session
  session.save();
  //now you can read session.foo from your express routes or connect middlewares
});

Namespacing

sessionSockets.of('/chat').on('connection', function (err, socket, session) {
  //the socket here will address messages only to the /chat namespace
});

Get session for a client

io.sockets.clients().forEach(function (socket) {
  // so far we have access only to client sockets
  sessionSockets.getSession(socket, function (err, session) {
    // getSession gives you an error object or the session for a given socket
  });
});

Error handling

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.

Troubleshooting

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.

Cookie lookup precedence

When looking up for the cookie in a socket.handshake, SessionSockets will take precedence on the following order:

  1. secureCookies
  2. signedCookies
  3. cookies

Specifying a session store key

You can specify your own session store key

new SessionSockets(io, sessionStore, cookieParser, 'yourOwnSessionStoreKey');

It defaults to 'connect.sid' (which is default for both connect and express).

A more detailed example

var http = require('http')
  , connect = require('connect')
  , express = require('express')
  , app = express();

Below are the two main references you will need to keep

var cookieParser = express.cookieParser('your secret sauce')
  , sessionStore = new connect.middleware.session.MemoryStore();

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.

app.configure(function () {
  //hiding other express configuration
  app.use(cookieParser);
  app.use(express.session({ store: sessionStore }));
});

Then you create the server and bind socket.io to it (nothing new here)

var server = http.createServer(app)
  , io = require('socket.io').listen(server);

Now instead of listening to io.sockets.on('connection', ...) you will pass it together with the sessionStore and cookieParser

var SessionSockets = require('session.socket.io')
  , sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

Which will use it all together to get you what matters

sessionSockets.on('connection', function (err, socket, session) {
  //your regular socket.io code goes here
  //and you can still use your io object
});

License

(The MIT License)

Copyright (c) 2012 Wagner Camarao <[email protected]>

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.