Comments (8)
you can try clustering with roomId as hashkey, this would limit your broadcast within that cluster.
ie- you will have three level approach here. RoomGroup -> Room -> sessionId.
from socket.io-redis-adapter.
This doesn't currently work in standalone mode, but we could indeed add this
subscriptionMode: dynamic
for the classic adapter. Let me get back to you.
this would be huge
from socket.io-redis-adapter.
Hi! That's a great question 👍
In a chat group of 500 users, a user sends "hello", db fetches the userIDs of all those users, and for each user Redis will ask ALL 32 node processes if they have that user's room.
You could also assign a room to this chat group, so that you can simply call io.to("the-chat-group").emit("hello")
instead of looping over each user ID.
Besides, a new adapter based on Redis sharded PUB/SUB (requires Redis v7) has been added in version 8.2.0.
More information here: https://redis.io/docs/interact/pubsub/#sharded-pubsub
There are two subscription modes:
- "static": 2 Redis channels per namespace
Useful when used with dynamic namespaces.
- "dynamic": (2 + 1 per public room) Redis channels per namespace
The default value, useful when some rooms have a low number of clients (so only a few Socket.IO servers are notified).
from socket.io-redis-adapter.
Hi @darrachequesne, instead of broadcasting to all node processes for every room message, we could optimize this by having each node process subscribe to the Redis channels that correspond to the rooms they have active socket connections for. This way, we can minimize unnecessary queries across all node processes and make the broadcasting more efficient.
Essentially:
- When a socket joins a room: The corresponding node process subscribes to a specific Redis channel for that room.
- When a message is sent to a room: The message is published to the Redis channel for that room.
- Handling the message: Only the node processes that are subscribed to that particular room's channel will receive the message from Redis and can forward it to the connected sockets.
This approach ensures that messages are only sent to the node processes that actually contain the targeted room, thus reducing the number of commands and improving scalability.
Would you consider this a viable solution for the scenario I described with a large number of nodes? Also if my understanding about the current way this package is incorrect, please let me know! Really appreciate your thoughts on this, and thank you for considering.
from socket.io-redis-adapter.
@marialovesbeans what you are describing is exactly how the new adapter works:
import { Server } from 'socket.io';
import { createClient } from 'redis';
import { createShardedAdapter } from '@socket.io/redis-adapter';
const pubClient = createClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();
await Promise.all([
pubClient.connect(),
subClient.connect()
]);
const io = new Server({
adapter: createShardedAdapter(pubClient, subClient, {
subscriptionMode: "dynamic"
})
});
io.listen(3000);
Thanks to the sharded PUB/SUB, the message are only forwarded to the right Redis nodes.
We will update the documentation on the website to make it clearer. It is not backward compatible with the previous implementation though.
from socket.io-redis-adapter.
Hi @darrachequesne, thanks for the quick response! Would this work for a simple single-instance Redis (non cluster mode)?
from socket.io-redis-adapter.
This doesn't currently work in standalone mode, but we could indeed add this subscriptionMode: dynamic
for the classic adapter. Let me get back to you.
from socket.io-redis-adapter.
Hi @darrachequesne - We were wondering whether this has been applied to the classic adapter (single instance, non-cluster mode)?
from socket.io-redis-adapter.
Related Issues (20)
- Retrieving number of connected sockets using `fetchSockets` without getting all socket information HOT 5
- Make subscription client optional HOT 1
- Proper way to use this adapter with ioredis HOT 2
- Feature request: limit nodes hit by "serverSideEmit" and "fetchSockets" in sharded adapter
- Error on websocket multi instance using redis-adapter HOT 1
- This.io.sockets.adapter.pubClient.isReady becomes false and is never ready again HOT 2
- Error: 23984 trailing bytes HOT 5
- dynamic subscription mode breaks remote socket.emit() HOT 3
- [email protected]: timeout reached while waiting for fetchSockets response
- MaxListenersExceededWarning due to SSUBSCRIBE/SUNSUBSCRIBE memory leak HOT 2
- the below error occurs when the user disconnect from the socket HOT 1
- the below error occurs when the user disconnect from the socket HOT 1
- Socket.io adapter HOT 1
- TypeError with @socket.io/redis-adapter on Node.js v18.15.0 HOT 5
- Lot of "ignoring unknown request" in Debug HOT 5
- Unnecessary psubscribe Operations in Namespaces Without Local Clients Increasing Redis Pub/Sub Channels HOT 2
- Unnecessary on("error") handlers
- Class extends value undefined is not a constructor or null HOT 3
- Cannot install package with 'npm install @socket.io/redis-adapter' HOT 1
- fetchSockets doesn't work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socket.io-redis-adapter.