Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Going Realtime with Socket.IO

Reza Akhavan edited this page Oct 20, 2013 · 20 revisions

The goal of this page is to explain how to add realtime functionality to your Drywall project. We'll be using the npm/socket.io and npm/passport.socketio modules. We are not Socket.IO experts by any stretch. There is surely room for improvement. We really want to demonstrate how to connect the dots in a Drywall app. If you have suggestions or feedback please let us know.

npm/socket.io by LearnBoost:

Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.

https://npmjs.org/package/socket.io

npm/passport.socketio by jfromaniello:

Access Passport.js user information from socket.io connection.

https://npmjs.org/package/passport.socketio

Step #1 Install the Modules

$ npm install socket.io --save
$ npm install passport.socketio --save

Step #2 Update our /app.js File

Right after we created our express app and web server, we connect Socket.IO.

...
//create express app
var app = express();

//setup the web server
app.server = http.createServer(app);

//setup socket.io
app.io = require('socket.io').listen(app.server);
...

Now where we configure passport, we need to extend the arguments, so that the express is in scope for /passport.js. Then after we define our normal express routes, we'll define our socket routes (more on this in Step #4).

...
//config passport
require('./passport')(app, passport, express);

//route requests
require('./routes')(app, passport);

//route sockets
require('./sockets')(app);
...

Step #3 Update our /passport.js Configuration File

Remember in Step #2 how we added the express argument so it was in scope here? Well don't forget to define that argument like so:

'use strict';

exports = module.exports = function(app, passport, express) {
...

Now before our passport.serializeUser method we setup the passport.socketio module, linking the express.cookieParser, app.get('crypto-key') and app.sessionStore. This makes it possible to access our Passport sessions during our socket handling logic.

...
var socketPassport = require('passport.socketio');

app.io.set('authorization', socketPassport.authorize({
  cookieParser: express.cookieParser,
  key: 'connect.sid',
  secret: app.get('crypto-key'),
  store: app.sessionStore,
  fail: function(data, accept) {
    accept(null, false);
  },
  success: function(data, accept) {
    accept(null, true);
  }
}));

passport.serializeUser(function(user, done) {
...

Step #4 Create our new /sockets.js Router

It's important to realize that sockets are event based, not path based like URLs. It is however easy to think of functionality in terms of file paths. And without Socket.IO, that's how our Drywall app is already structured (think routes).

*WIP*

Use the Force

I hope this was helpful. If you have questions or think this page should be expanded please contribute by opening an issue or updating this page.