Mithun Sarker
Mithun Sarker

Reputation: 4023

How can I emit a message via socket inside express router

I am trying to emit a message via socket.io . I want to do it when a express router is called .

var express         =   require("express");
var app             =   express();
var http            =   require('http').Server(app);
var io              =   require('socket.io')(http);
var bodyParser      =   require("body-parser");
var router          =   express.Router();

router.route("/test").post(function(req,res){
    console.log(req.body)
    response = {"error" : false,"message" : "api worked"};
    /*I want to emit a message via socket here */
});


app.use('/',router);
app.listen(3010);
console.log("Listening to PORT 3010"); 

How can I do , I have seen a lot of example of using socket . But I couldn't find a way to do what I want . I have also checked the following question . But I couldn't accomplish it

How to use socket.io in express routes?

Upvotes: 1

Views: 492

Answers (1)

jfriend00
jfriend00

Reputation: 707318

Posting my comment as an answer since it worked for you.

io.emit("someMsg", someData);

will send to all connected users. Your clients will, of course, need to be connected with socket.io in order to receive the message.

Upvotes: 2

Related Questions