Skip to content Skip to sidebar Skip to footer

Node Socket.io Passing Socket To Other File

I'm building my app and to make things simple let's say I have main file called app.js and file called Rooms.js in which I want to have all the functions to handle socket.io rooms

Solution 1:

I found more elegant solution which satisfies me: in app.js (which i renamed for clarity to Server.js) i added to

var server = http.createServer(app.callback());
var io = socketio.listen(server);

the following:

exports.server = server
exports.io = io

And into io.sockets.on('connection', function(socket){ ... } I added exports.socket = socket.

This allowed me to use Server.js module in /methods/Rooms.js:

var Server = require('../Server')

and later on in this file I simply had to change socket.*, io.* syntax into Server.socket.*, Server.io.*.

Post a Comment for "Node Socket.io Passing Socket To Other File"