Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created October 5, 2013 10:09
Show Gist options
  • Save soyuka/6839092 to your computer and use it in GitHub Desktop.
Save soyuka/6839092 to your computer and use it in GitHub Desktop.
Handle sockets in a different file node.js with expressjs
/* app.js */
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('./utils/sockets').listen(server); //for example
/* utils/sockets.js */
var socketio = require('socket.io');
module.exports.listen = function(app) {
io = socketio.listen(app);
io.set('log level', 0); //Set less log
io.sockets.on('connection', function (socket) {
socket.on('getSomething', function(datas) {
//Broadcast only to client !
io.sockets.socket(socket.id).emit('sendSomething', JSON.stringify(datas));
});
});
return io;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment