1

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?

Community
  • 1
  • 1
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64

1 Answers1

2

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979