Is it ok to use many socket.io events? - node.js

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.

Related

How to avoid users see data from other user socket?

I have a dasboard that is making socket request every five seconds, sometimes, some users start getting data from other user socket request, but at the begging everything is working fine.
I have tried with sticky-session, diferrent socket instance, personalized socket event names.
if someone unsderstand my problem and i have a solution, i would be grateful.
Sockets are, by definition, separate from each other. I suspect the issue is that you're emitting to a namespace rather than to a particular socket.
io.of('someNamespace').emit('data');
vs
io.of('someNamespace').on('connection', (socket) => {
socket.emit('data');
});
In the first example we're sending data to all sockets in the namespace. In the second we're only sending data to a particular socket. The difference is in where you're emitting the data.

Is there a possible way to restrict processing of socket emits and make delay between them?

For example: to prevent user spam in chat room, is there a socket.io server side solution that could prevent user from emitting event if 3 seconds for example haven't passed yet?
On a rough look, it seems you'd need to implement any filtering like this client-side, the docs don't seem to show any hooks for message filtering. You could have your clients send to a different channel than the one they listen to, then setup a relay on the server that listens to one channel, filters then emits to another...

Socket.io - how to use listeners

I've looked around for an answer regarding this question, I am not sure if I am going about this the right way.
I have a node.js application that uses socket.io to push and receive data from the node server to the client. Most of the requests sent to nodejs are through HTTP Requests while the data pushed to the website is received through a socket.
I currently have unique namespaces for each of the individual listeners (for example I have a feed with comments, this means I have feed/{ID} as the listener as well as each comment has comment/{COMMENTID}. This means if there are 25 feed posts, I would have 26 (including the feed) listeners listening for the feed alone.
I am not entirely sure how socket.io pushes data through the listeners (is each listener a new socket?). In my mind, if I have a large amount of users online at one point and a single comments listener it will be hit many times with useless, unrelated data - in comparison to now where it will only receive data relevant to the user.
What is the best way to set up the listeners?
Is more or less listeners beneficial?
That's a bad way to use listener. You should use just
socket.on('feed',feed)
socket.on('comment',comment)
When you want to send data to feed listener, use "socket.emit('feed',{id:1})".
When you want to send data to comment listener, use "socket.emit('comment',{commentid:1})"
This will reduce you to just 2 listener.
You should use Rooms to handle this. Each time a user is viewing a feed page, it register to a room and then you push only the relevant information to users based on the page they are actually seeing.
socket.on('subscribe', function(data) { socket.join(data.room); });
socket.on('unsubscribe', function(data) { socket.leave(data.room); });
then when you want to send information to a specific room
io.sockets.in('feed_1').emit('comment', data);
You can see the documentation here: https://github.com/LearnBoost/socket.io/wiki/Rooms

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.

Bind event to all socket.io connections

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.

Resources