Ruby On Rails - Sharing Session with Node.js

Nov 29, 2012
• Ruy Rocha

I’ve spent last five or six days working with Node.js, and got a problem: main application was written in Ruby On Rails, and node.js plus Socket.IO just are doing the real time part of it, so how could I access Rails’ session data in node.js?

When you save your sessions into a database, ActiveRecord::SessionStore stores marshalized data by default and you can’t simply decode this data from base64 and use it. After Googling around, node’s community says something like to do not try to decode or unmarshall session data: this information should be stored as JSON object. I’ve hacked – without success – marshall and unmarshall SessionStore class methods in my first attempt to store data as JSON – session was saved but I’ve broke everything on Rails side. In fact, when data was retrieved with new unmarshall method, it was returning an Array for flash messages, and this was wrong because ActionDispatch::Flash expected a Hash. I’ve ended up with this initializer and now I’m able to share Rails’ sessions with node.js (and probably with any other framework or language):