Can I receive a response from a socket.emit request without setting up a handler for the reponse?
Date : March 29 2020, 07:55 AM
wish of those help This isn't possible given the inherent asynchronous nature of socket communication. As soon as your first 'emit' is called, flow has moved on. This is mostly desirable as you'd want to avoid 'locking up' your application while it waited to receive a response.
|
How to emit an event in response to an event using socket.io?
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , It appears to be ok. But, just in case, check this answer with all ways of sending data to clients and try use "io.emit" (to send to all clients). Or try sending a string instead of a json object. I have tried this example here and it is working, just copied and pasted.
|
How to emit a socket.io response within post request in NodeJS
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further 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. , Socket is locally scoped. io.sockets is not, so you can do: app.post('/api/datacreated/', function(req, res) {
//this is coming from an external resource
io.sockets.emit('data created', data);
})
|
Do socket.broadcast.emit('myMessage', data') emit the message to all clients even if some have not set listener to 'myMe
Date : March 29 2020, 07:55 AM
Any of those help Do socket.broadcast.emit('myMessage', data') emit the message to all clients even if some have not set listener to 'myMessage'?
|
Is it possible to emit a event within a event listener callback for 'newListener' event in nodejs?
Date : March 29 2020, 07:55 AM
it fixes the issue The problem is that inside the newListener a new listener is not yet active. Use the process.nextTick(): const EventEmitter = require('events');
var eventEmitter = new EventEmitter.EventEmitter();
eventEmitter.on('newListener', function(event) {
if (event === 'data') {
process.nextTick(() => {
eventEmitter.emit('data', 'testing');
});
}
console.log('newListener', event);
});
eventEmitter.on('data', function(test) {
console.log('data', test)
});
|