Bind event to all socket.io connections - node.js

I know it's possible to emit events to all connections using io.sockets.emit('some event'), but can I also globally bind events to all present and future connections? Would probably look like
io.sockets.on('feedTheDog', function (data) {
// Someone told me to feed the dog..
});
Also, can I remove event listeners with .removeAllListeners() with Socket.IO?

I also don't think you can bind events to future connections, but yes, you can use removeAllListeners with socket.io. They ported node's event emitter to the browser, so it's exactly the same api there. Here's the code for it: https://github.com/LearnBoost/socket.io-client/blob/master/lib/events.js

You can use socket.io-events and attach middleware functions to handle messages for all socket connections, without actually attaching a handler to a socket.

Related

Callbacks are not supported when broadcasting

Here I am facing one issue with Callbacks in socket.io with nodejs
io.sockets.to(usersocketid).emit('receivemsg',
{'success':'1','data':message},function(deliverycb){
console.log('delivery call back');
console.log(deliverycb);
});
Where i am facing error of Callbacks are not supported when broadcasting so anyone have solution of this.
Since you are using callback for acknowledgement,and according to socket.io documentation.
Acknowledgements are not supported when broadcasting.
Acknowledgements are not supported when emitting from namespace.
I would say, if you need to send acknowledgement to the server, then emit another event from the client-side.
First, get socket object from id, which you can refer to Socket.IO 1.0.x: Get socket by id
Then with the socket object, socket.emit('type', 'message', ack =>{});
It's possible you were trying to emit the acknoledgement from the server side, without specifying the socket.id;
By the way, you can avoid that error, moving the acknowledgement request to the clint-side and just work the callback on the server-side.

Does socket.io allow registering different handlers for one single event?

When describing receiving events, socket.io document doesn't say whether I can use more one event handler for the same event or not. For example, I have two components both need to handle io.on('connection' ...
Is that possible?
Does socket.io allow registering different handlers for one single event?
Yes, it's just an EventEmitter like object and you can have as many handlers for a given event as you want. Specifically, there is no problem having more than one of these:
io.on('connection', ...)

How to get array of native events from socket.io?

I'm writing some 'shield' code in my node websocket server, so that if the client tries to send events which are out of state, the socket is disconnected.
But since socket.io handles their native events (like 'connecting', 'disconnected' etc) similar to user defined events, I'm having a hard time differentiating between them in my generic handler
Is there a way to programatically retrieve a list of all built-in events from socket.io?
I want to avoid hardcoding this list.
After some digging, find out there is internal list of packet names, and is easily accessible:
var io = require('socket.io');
var events = io.parser.packets;
It has a bit more than just three usual events, but can be usefull anyway.

How do I completely destroy a socket.io connection?

I'm creating a browser chat from Socket.io and Node.js. Everything has been running smoothly, but I appear to be having a problem with disconnecting sockets. When I run socket.disconnet();, the server runs the socket.on("disconnect", event, but it doesn't actually remove the socket from internal listeners.
When I run socket.disconnect(); on a socket, the socket no longer recieves any new messages, but when the "disconnected" user sends a message, the server receives and sends it back to all clients. I want to create a proper /kick command but it's difficult when I have to restructure all of my code just to accomidate for a simple function.
Commands like socket.connection.destroy();, socket.end();, and socket.transport.destroy(); are invalid and undefined. Does anyone have any suggestions? I've been working on this problem for days and I haven't found any answer other than to set a shutup boolean to the socket and tell the message event to ignore specific sockets. Is this the best way? What happens if the user starts editing javascript code and I need a way from receiving other events from a client?
Well you can see if the socket is connected or not. If socket is connected you can emit the data and vice versa. :)
Hope it helps..!
YourProject.sockets.on('connection',function(socket){
setInterval(function(){
if(!socket.disconnected){
socket.emit('entrance',{message:'Hey Bro'});
}
},10000);
});
have you tried to interrupt thread ? That should end all I/O operations with an Exception.

Is it ok to use many socket.io events?

In socket.io for node.js you create events using:
socket.on('my event', function (data) {...});
In my case I may need to use a lot of different events (close to a 100), so I'm wondering if each of these events creates a separate listener for each client socket and would take more resources than just having a single event that receives an object that contains and identifier on which I can use switch for the events I require. Which option would be better?
They will not create new sockets for each event, you can send as many events as you want. When the socket packet goes down the wire, the event name is just a text property.
If you use your own event-name system, then you will probably just be rewriting the one already there.
There should not be an issue having over 100 events for a socket.io connection.
Each even does not create a new connection, so there should not be a "too many connections" issue.

Resources