LoginSignup
0
0

More than 5 years have passed since last update.

Socket emit inside functions

Posted at
// config/lib/socket.io.js
{
...
  // Add an event listener to the 'connection' event
  io.on('connection', function (socket) {
    config.files.server.sockets.forEach(function (socketConfiguration) {
      require(path.resolve(socketConfiguration))(io, socket);
    });
  });

// added this line below
app.set('socketio', io);

return server;
}

Second, I load this variable as 'app.get' which is like snipplet below:

// any server controller file, I want to emit, 
// in my case, user.profile.server.controller.js
{
var io = req.app.get('socketio');

...
io.emit('rom1', msg);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0