Socket.io emit on Express Route

2019-01-17 05:10发布

I want to emit some data to the client when some API route gets called. I have to following code on server.js

var app  = express();
var http = require('http').Server(app);

var io   = require('socket.io')(http);

io.on('connection', function(socket){
  console.log('a user connected');

  socket.emit('tx', 'msg');

  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});

Now I have this route /test:

var bsg   = require('./routes/test');

And that file:

var express      = require('express');
var passport     = require('passport');
var router       = express.Router();

router.get('/test',  function(req, res) {
  //work here
});

module.exports = router;

On client side:

<script type="text/javascript">
   var socket = io();

   socket.on('tx', function(data) {
     console.log(data);
   });
</script>

Whats the best solution for this?

Thanks!

Express 4 / socket.io 1.4.5

2条回答
Deceive 欺骗
2楼-- · 2019-01-17 05:37

You can attach the instance to req object also:

app.use(function(req,res,next){
req.io = io;
next();
})

And to emit to a single client

req.io.to(<socketId of cient>).emit(data);
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-17 05:55

Attach the io instance to your app.

app.io = io;

Then you can access it via the request.

router.get('/test',  function(req, res) {
  req.app.io.emit('tx', {key:"value"});
});

If you are expecting to emit data to a single client you would need keep some kind of session mapping to link a standalone http request to a socket. A session ID might not be a one to one mapping though as you can have many sockets open for one session. You are better off handling a request/response pattern directly with socket.io callbacks.

查看更多
登录 后发表回答