I
I
izac2016-02-22 06:04:42
JavaScript
izac, 2016-02-22 06:04:42

How to replace the $emit method (which used to be in socket.io)?

Example: otherModule.js

exports.post = function (req, res, next) {
  var sid = req.session.id;
  var io = req.app.get('io'); 

  req.session.destroy(function (err) {
    io.sockets.$emit('session:reload', sid);
    // ...
    res.redirect('/');
  }
};

Handler in socket.js
io.sockets.on('session:reload', function(sid) {
  // ...
});

You just need to generate an event from another module for further processing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
izac, 2016-02-22
@izac

Found I don't think this is the best way, but correct me if it's wrong
in socket.js

io.on("sessreload",function(sid){
//some code
});

in otherModule.js
exports.post = function(req, res,next){
  var sid = req.session.id;
  var io = req.app.get("io");
  req.session.destroy(function(err){
    io.sockets._events.sessreload(sid);
    if(err) next(err);
    res.redirect('/');
  });
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question