Rayjax
Rayjax

Reputation: 7784

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 :)

Upvotes: 6

Views: 1084

Answers (1)

Wes Johnson
Wes Johnson

Reputation: 3101

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.

Upvotes: 1

Related Questions