Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle sessions in node.js without frameworks

I'm working on a SPA (single page web app); the idea was to go lightweight and not use too much frameworks and abstraction and stuff, so I created the HTTP server for static+dynamic files and it works well. Now I have implemented socket.io in the web app, but I would like to know what in your opinion would be a good way of handling sessions (keeping in mind that socket io must be able to identify the user who calls functions and know to who it must push data). Hope i've been clear enough :)

like image 269
Rayjax Avatar asked Sep 30 '12 19:09

Rayjax


1 Answers

Socket.io has built in methods for saving server-side session data for a given socket via socket.get, socket.set and socket.del. Where it saves this data is by default a memorystore, but you can use redis, etc. Keep in mind that when the socket disconnects, that data doesn't persist on reconnect, so you'll want to send along client identifying data with your socket setup events or during auth.

So that leaves your client data, which can be persisted via localStorage, sessionStorage, or plain old vanilla cookies, among others.

like image 85
Wes Johnson Avatar answered Oct 04 '22 02:10

Wes Johnson