Use socket.io in controllers

2019-03-08 01:12发布

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.

1条回答
太酷不给撩
2楼-- · 2019-03-08 01:41

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

// no require needed here, at least, I don't think so

// Controller agrees to implement the function called "respond"
module.exports.respond = function(socket_io){
    // this function expects a socket_io connection as argument

    // now we can do whatever we want:
    socket_io.on('news',function(newsreel){

        // as is proper, protocol logic like
        // this belongs in a controller:

        socket.broadcast.emit(newsreel);
    });
}

Now in server.js:

var io = require('socket.io').listen(80);
var controller = require('./mycontroller');

io.sockets.on('connection', controller.respond );

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

module.exports.respond = function(endpoint,socket){
    // this function now expects an endpoint as argument

    socket.on('news',function(newsreel){

        // as is proper, protocol logic like
        // this belongs in a controller:

        endpoint.emit(newsreel); // broadcast news to everyone subscribing
                                     // to our endpoint/namespace
    });
}

Now on the server we'd need a bit more work in order to pass the end point:

var io = require('socket.io').listen(80);
var controller = require('./mycontroller');

var chat = io
  .of('/chat')
  .on('connection', function (socket) {
      controller.respond(chat,socket);
  });

Notice that we pass socket straight through but we capture chat via a closure. With this you can have multiple endpoints each with their own controllers:

var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');

var news = io
  .of('/news')
  .on('connection', function (socket) {
      news_controller.respond(news,socket);
  });

var chat = io
  .of('/chat')
  .on('connection', function (socket) {
      chat_controller.respond(chat,socket);
  });

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:

var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');

var chat = io
  .of('/chat')
  .on('connection', function (socket) {
      news_controller.respond(chat,socket);
      chat_controller.respond(chat,socket);
  });

It even works with plain socket.io (no endpoints/namespaces):

var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');

io.sockets.on('connection', function (socket) {
    news_controller.respond(socket);
    chat_controller.respond(socket);
});
查看更多
登录 后发表回答