uglycode
uglycode

Reputation: 3082

How to call socket.io's emit from express.js route?

I'm using express.js and socket.io and I want to achieve that when the new data arrives at one of the routes, socket emits the info to subscribers.

Something in this manner:

    app.get('/:id', function(req, res){
       usersFactory.add({
         id: req.params.id
       });
       //HOW TO EMIT userFactory.getAll() HERE???
       res.json({status: 'success'});
    });

The complete code is available here: https://gist.github.com/javascrewpt/c28565cfd307073675f3

Thank you for your time!

Upvotes: 1

Views: 565

Answers (1)

jfriend00
jfriend00

Reputation: 708116

If you want to send a message to all connected sockets using socket.io, you would use:

io.sockets.emit(msg, data);

If that is not what you're trying to do, then please describe in words exactly what you want help accomplishing.

Upvotes: 2

Related Questions