admin管理员组文章数量:1387410
I have a node server running a react application, integrated with Socket.io. All works fine. I also have an external application that uses the same data. What I want to do is, when the data changes, post to my node server and then in the response, emit to any data to users who are currently subscribed to the socket.
//SERVER CODE LIVES ABOVE HERE...
const io = require('socket.io')(server);
io.on('connection', (socket) => {
socket.on('create data', function(data) {
//a place to create data, once plete triggers everyone subscribed to get an update.
socket.emit('data created', data);
});
});
app.post('/api/datacreated/', function(req, res) {
//HOW DO I EMIT IN HERE?! - this is ing from an external resource
socket.emit('data created', data);
})
I have a node server running a react application, integrated with Socket.io. All works fine. I also have an external application that uses the same data. What I want to do is, when the data changes, post to my node server and then in the response, emit to any data to users who are currently subscribed to the socket.
//SERVER CODE LIVES ABOVE HERE...
const io = require('socket.io')(server);
io.on('connection', (socket) => {
socket.on('create data', function(data) {
//a place to create data, once plete triggers everyone subscribed to get an update.
socket.emit('data created', data);
});
});
app.post('/api/datacreated/', function(req, res) {
//HOW DO I EMIT IN HERE?! - this is ing from an external resource
socket.emit('data created', data);
})
Cheers for any time you've got!
Share Improve this question asked Jun 27, 2017 at 9:12 MattJHoughtonMattJHoughton 1,12813 silver badges19 bronze badges2 Answers
Reset to default 6Socket is locally scoped. io.sockets is not, so you can do:
app.post('/api/datacreated/', function(req, res) {
//this is ing from an external resource
io.sockets.emit('data created', data);
})
the solution that was working for me
const io = new Server(...)
global.io = io
app.post('/api/datacreated/', function(req, res) {
global.io.emit('data created', data);
})
本文标签: javascriptHow to emit a socketio response within post request in NodeJSStack Overflow
版权声明:本文标题:javascript - How to emit a socket.io response within post request in NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744541700a2611652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论