I'm trying to emit a message in a specific room in my modules following Patrick Robert's answer How can i export socket.io into other modules in nodejs?.
io.js
var sio = require('socket.io');
var io = null;
exports.io = function () {
return io;
};
exports.initialize = function(server) {
return io = sio(server);
};
module.js
ioreq = require('./io')
ioreq.io().on('connection', function(socket) {
var room_name = 2;
socket.join(room_name);
socket.to(room_name).emit("test",'This is a Test'); //This is working
});
function sendDataFromModule(data) { // This function don't do anything at all !
socket.to(room_name).emit("test",data);
}
sendDataFromModule('Test from function');// Not working
}
Can you help me make it work ?