Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What's the best way to implement socket.io as a submodule within ExpressJS?

What's the best way to implement socket.io as a submodule within ExpressJS?

Problem

I'm trying to build an ExpressJs based app using the Submodule architecture suggested by tjholowaychuk.

I'd like to also have some realtime socket interaction, so I'm trying to integrate socket.io. I'm struggling to find the best way to do this as a sub app and allow both Express and socket.io listen together.

Problem courtesy of: Dan Steele

Solution

You could put all socket.io related code in a separate file:

// socketio.js
var sio = require('socket.io');
module.exports = function(server) {
  var io = sio.listen(server);
  io.sockets.on('connection', ...);
  return io;
};

// app.js
var app    = require('express')(), 
    server = require('http').createServer(app),
    io     = require('./socketio')(server);
Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What's the best way to implement socket.io as a submodule within ExpressJS?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×