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) {
...

From here, we're going to create a super simple chat room feature on the /about/ page of Drywall.

Step #4 Create our new /sockets.js Router

'use strict';

exports = module.exports = function(app) {
  app.io.sockets.on('connection', function(socket) {
    socket.on('/about/#join', require('./events/about/index').join(app, socket));
    socket.on('/about/#send', require('./events/about/index').send(app, socket));
  });
};

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

Step #5 Create the Server-Side Event Logic

Create the file /events/about/index.js with the following code:

'use strict';

exports.join = function(app, socket){
  return function() {
    socket.visitor = 'guest';
    if (socket.handshake.user) {
      socket.visitor = socket.handshake.user.username;
    }
    
    socket.join('/about/');
    socket.broadcast.to('/about/').emit('/about/#newUser', socket.visitor);
  };
};

exports.send = function(app, socket){
  return function(message) {
    socket.broadcast.to('/about/').emit('/about/#incoming', socket.visitor, message);
  };
};

Thanks to the npm/passport.socketio module we setup eariler, we can use socket.handshake.user to get access to our Passport session.

It's worth noting that we're putting this code in the /events/ directory and not the /views/ directory. These are not views, they don't render any templates and they shouldn't cloud that part of our app logic.

*WIP* - this page is under construction

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.