Question

I'm building a realtime mobile app (native) and I'm interested in starting the app from a user login screen and then move on.

I figured I need Express + primus with socket.io (or sockjs) + passport.socketio + redis (not 100% sure I need redis yet) to build my backend.

Node.js placeholder

I even found this step by step tutorial which is really helpful, it takes me step by step to making a secure api.

My question is a double one:

  1. How can I tweak this example to use TOKENS instead of cookies (since I'm building a native mobile app and not a browser web app) and its more secure according to this.
  2. How to bind express with socket.io - in other words, how does socket.io get to know if the user is authenticated or not?

I welcome any comment or advice.

Thank you.

Was it helpful?

Solution

First, I would use a different websocket library instead of socket.io. The socket.io developers are currently working on engine.io and socket.io appears to not be very actively maintained. I've experienced many of the issues described in the following links and since moving to sockjs have not had any problems.

http://www.quora.com/Sock-js/What-are-the-pros-and-cons-of-socket-io-vs-sockjs?share=1 https://github.com/LearnBoost/socket.io/issues https://github.com/ether/etherpad-lite/issues/1798 http://baudehlo.com/2013/05/07/sockjs-multiple-channels-and-why-i-dumped-socket-io/

You may have to implement your own custom events on top of sockjs, but that's pretty trivial. Since it sounds like you're already using redis then implementing rooms and pub/sub should be pretty easy too.

Here's how we do our token based socket authentication.

  1. First the client makes an HTTP request to the server to request a token. This routes the request through express' middleware and gives you easy access to the session data. This is where you would interact with passport to access their account or session data. If the user is logged in then generate a UUID and store their session data in redis as a key/value pair where the key is the UUID and the value is their stringified session/account data. Only send the UUID back to the client.
  2. When the client first creates a websocket connection set a flag on the socket that marks it as unauthenticated.
  3. When a message comes in on a socket check to see if the socket is authenticated. If not then check for a token in the message. If it doesn't exist then kill the connection. If it does then query redis for the key/value pair keyed by the token. If redis returns any data then you now have the session data for that user and can attach it to the socket and mark the socket as authenticated. If there's nothing in redis keyed by the token then kill the connection.

Now when you perform any operations on a socket you should have access to the session data for that user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top