Managing Sessions in Node.js? [closed]

What is the best way to manage session variables in Node.js? Is there any library?


Solution 1:

You can do that very easily using:

  • Connect: http://senchalabs.github.com/connect/

    Connects is like Rack in Ruby. It gives you an extra layer where you can "play" with authentication, sessions, cookies, among others.

Other option is to use frameworks:

  • Express.js: http://expressjs.com/

    It seems to be the most used node.js framework. Is like Sinatra for Ruby and runs on top of connect.

  • Geddy: http://geddyjs.org/

    If you want to do more complex WebApps, Geddy is the one you choose. Is like Rails for Ruby.

Solution 2:

Just offload it to memcache or some other caching mechanism. I wouldn't burden your servers with this sort of thing. What is the point of a super lean web server that has to remember stuff.

I would also try and develop your site as an application and not a website, or treat your website as an application, use the wonderful features of html5 such as local storage/local databases and cut down on the amount of traffic between server and client machines.

If all else fails (or site is small) then what's stopping you write your own session class. Not that difficult. Especially if its an in memory type thing. Put some timer logic to time out sessions and there you go. Damn in a dynamic language such as JavaScript, should be a cinch.

Structure should be a dictionary with key being session and value being an object containing details of last communication and capabilities (to enable access to certain features). Add a sweep function to clear out old sessions that have timed out. and bingo. A basic session service. a basic check on "is session key in list...yes/no...get details"...and I think thats it....or is there some feature that I am missing.

I personally would avoid any third party tool out there for as long as possible. Sands of time shift very quickly and you can always depend on code developed by yourself.

Solution 3:

nodejs provides a basic http API. http is stateless, and ideas of sessions and session variables exist in framework/protocols build on top of http. http://en.wikipedia.org/wiki/Session_%28computer_science%29

Take a look at http://geddyjs.org/ or http://expressjs.com/ as examples of web frameworks built with node that provide sessions.

Solution 4:

Donald's answer is good - once you get into the onion pattern of connect middleware you have to make a decision on what type of session store to use. The default one in express is a MemoryStore, and is not intended for production use. Here are some of your choices:

Mongo https://github.com/mikkel/express-session-mongo - Be sure to use the option 'native_parser:false'

Redis https://github.com/visionmedia/connect-redis - Very good, but if you aren't already using redis for pub/sub or storage it might not be ideal.

Note, there are other choices - it depends on your project. Look for something you can introduce leveraging your existing technology stack.

Solution 5:

If you are looking for serious web development using Node.js, use Express framework; it supports sessions.

Create the Express project with the --sessions options.

$ express --sessions

To install Express:

$ npm install express -g