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.
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.
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.
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 .
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.
Let's take a look at an example HTML page:
- Which artist sang the song Hey Brother
- What is SEC Regulation SK
- What is the future of video
- How big is Trinity Western University
- What are the problems associated with globalization
- What was Tim Conways first TV appearance
- How do I develop my willpower easily
- What factors cause happiness at work
- What does Sukeerti mean in any language
- What is Alia Bhatt’s height
- What is Cedar Falls IA known for
- Whats the reason to see a psychologist
- What is brb in SMS language
- Sexuality Is Siddhartha malhotra gay
- What is the physical process of thinking
- How being a father changed you
- What are the beliefs of Romania