What is your review of Socket io


A simple library for Node.js that allows you to easily use your sessions from Connect, Express and others with Socket.IO.


How To Use

This library provides two methods of linking your session data with Socket.IO's connections. You can either reload the session from the store per-connection or per-message.

TL;DR Version

The basic idea is that on new Socket.IO connection, you auto-magically have a callback like instead of just , giving you access to your session store data.


This adds a new event which you can listen for using , namely . It looks something like this:


On the client side, you simply need to include a link to the JS file and then connect using instead of .

That's it! You're done. Everything else is as normal. Feel free to read on if you'd like a more detailed explanation though.

Server Configuration

Let's have a better look at some example usage. Assuming we have the following basic app, using Connect:

To allow Socket.IO to access our sessions, we insert the following:

This adds new events that we can listen for using the listener. Let's take a look at how we use these.

Per-connection session handling

This method loads the session from the store at the start of the connection, and then writes it on disconnect. You do this by adding a listener for the event, as follows:

It is worth noting that if your application crashes, then the post-disconnect callback will never be called. So any changes you made to the session while that connection was active would be lost.

Per-message session handling

This method will reload the session each time a message is received and then write it back to the store after firing the callback. You use this by passing the option, as follows:

Using a different session store (e.g. Redis)

This is as simple as swopping out the two lines in our server code above with:

Using Express or some other framework

If you were using Express, then your app declaration would look something like the following:

Expired session handling

There is an extra event called which is fired if the session isn't found in the session store or if there is an error retrieving it. This is handled as follows.

Cutting cookies

In each example above, we've included a callback. This callback is used so that when the browser requests the client Javascript file, it takes the session ID from its cookie and bundles this along with the client code.

If you aren't using Connect or Express however, then you may be handling sessions differently. In this case you need to pass a callback that will allow the server to get the session ID from the given HTTP Request object, as follows:

Here are two examples of such callbacks:

The session ID returned from this callback will be the ID used to retrieve the session data from the store using a call to .

Client Configuration

The client configuration is really straightforward. All that is required is that the client JS link is included and then your client code is as usual, except using .

The default path to the client JS is . Note that if you change the default path of Socket.IO from , using the setting, then this will also change the base path of this client JS.

Browser caching

It is possible that the browser might cache the Javascript file, which can cause problems if the session expires or the session ID becomes invalid, since this is served inline with the JS. In this case, just add a timestamp to the link. If you're using express, this is how you do it:

Example page

Let's take a look at an example HTML page: