I only need socket.io to emit messages to clients, if a new object is inserted to database. So my idea was to emit the message directly from my controller’s insert-method. In my server.js file, iam creating the socket.io object and try to make it accessible for other modules:
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
//make socket io accessible for other modules
module.exports.io = io;
In my controller i try using socket.io this way:
var io = require('../server').io;
...
io.sockets.on("connection", function(socket){
passportSocketIo.filterSocketsByUser(io, function (user) {
return user.workingAt === socket.handshake.user.workingAt;
}).forEach(function(s){
s.send("news", insertedObject);
});
});
And here iam stuck. The "connection" event will never be fired and so the message will not be emitted. Is that the correct way to use socket.io in seperate files? Unfortunately i cant find complex socket.io example.
You are trying to invert the flow of control. The way to do it is for your controller to implement an interface (an API) that your server can use to pass control to.
A simple example would be:
In
mycontroller.js
Now in
server.js
:This example is simple because the controller API looks exactly like a socket.io callback. But what if you want to pass other parameters to the controller? Like the
io
object itself or the variables representing end points? For that you'd need a little more work but it's not much. It's basically the same trick we often use to break out of or create closures: function generators:In
mycontroller.js
Now on the server we'd need a bit more work in order to pass the end point:
Notice that we pass
socket
straight through but we capturechat
via a closure. With this you can have multiple endpoints each with their own controllers:Actually, you can even use multiple controllers for each endpoint. Remember, the controllers don't do anything apart from subscribing to events. It's the server that's doing the listening:
It even works with plain socket.io (no endpoints/namespaces):