1

I've always been a bit of a Perl/PHP sorta guy, but I fancy a change and Node JS seems like the right place for me to go next.

I've watched a good few hours of tutorials on YouTube and read some posts on here - but I have come up a bit stuck.

I'd like to include socket.io in my express-generated application (v4.10.6). At the same time though, I don't really want to include the socket.on(...) statements in one file - i'd much rather split it out like you would with a route.

Given that the express-generated app is started in bin/www, i'm confused as to where I need to require('socket.io') and point all the 'on' events to.

This post on stackoverflow, I think may answer my question - but it suggests all the socket handlers are in the ./sockets/base.js file - and I am confused by the Gofilord's response to the answer.

Please forgive my ignorance here - this is all a bit alien to me at the moment, and thank you, as always for taking the time to read this and your help.

/bin/www

#!/usr/bin/env node
var debug   = require('debug')('rhubarb');
var app     = require('../app');

app.set('port', process.env.PORT || 1127);

var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});
Community
  • 1
  • 1
ash
  • 1,224
  • 3
  • 26
  • 46

1 Answers1

2

Its typical to require socket.io in app.js and then to tell your io sever to listen to your application. Using the example you posted, that would look like this:

var debug   = require('debug')('rhubarb');
var app     = require('../app');
var server  = require('http').Server(app);
var io      = require('socket.io')(server);

app.set('port', process.env.PORT || 1127);

var server = server.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});

The socketio docs do a really good job of explaining this. Here's an example from their homepage:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Update:

I typically modularize socketio setup by creating a lib called io.js in /lib with something like this:

module.exports = function(server){
  var io = require('socket.io')(server);

  // catch errors
  io.on('error', function(err){
    throw err;
  })

  // Set Socket.io listeners by creating a socket.io middleware
  // attachEventlisteners would live in `/controllers`
  io.use(attachEventlisteners);

  io.on('connection', function (socket) {

    // do things

  });

  return io; // so it can be used in app.js ( if need be )
}

then in app.js i can simply pass the server in when I require it:

  var io = require('./lib/io')(server);

You dont need to do any thing further in app.js since everything is setup in /lib/io.js, but if you wanted to you could because the io server is returned.

agconti
  • 17,780
  • 15
  • 80
  • 114
  • Thank you very much @agconti - I'm still trying to glue together in my mind. Now that socket.io is included in /bin/www - can I ask where to put that where can I put my *io.on('connection'...* code in a modular way for each page (eg "about", "contact" etc) please? – ash Dec 28 '14 at 18:53
  • @doublesidedstickytape These are really separate questions though. If both answers work for you, it'd be better to open up another question about modularizing socketio. That way we can help others who are running into the same issue. – agconti Dec 28 '14 at 19:25
  • I Need some time to digest your response and make sure I understand it; but I really appreciate the time you have spent to help me with this - thank you again @agconti :) – ash Dec 28 '14 at 19:37
  • I'm looking to modularise my socket io implementation, but i'm so confused. Is there any chance you can post an example of `io.use(attachEventlisteners);` so i can see how you'd hook into io from controllers? – Scotty May 12 '15 at 19:33