Comments (6)
I switched to using my own redis-pubsubber.
I use it by creating a channel per namespace.
Now the cpu's don't go above to 5% in the above tests.
from socket.io-redis-adapter.
👍
from socket.io-redis-adapter.
Hello,
I have the same issue in a project in which many namespaces are used with multiple Node.js instances.
@peteruithoven Could you explain how did you switch from socket.io-redis to your redis-pubsubber package in a multi-instances Socket.io app ?
from socket.io-redis-adapter.
This is basicly how I do it:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redisPubSub = require('redis-pubsubber')('myproject',6379,'localhost');
redisPubSub.on('error',function(err) {
console.log("myproject namespaces redis error: ",err);
});
createNSP("mynamespace");
function createNSP(nspName) {
var nsp = io.of("/"+nspName);
// create pub/sub channel for this namespace
var eventsChannel = redisPubSub.createChannel(nspName);
nsp.on('connection', function(socket){
// receive pub/sub events
function onEventsMessage() {
// emit incomming pub/sub broadcast to this socket
socket.emit.apply(socket,arguments);
// do something
}
// make all new socket connections listen to pub/sub messages
eventsChannel.on("message", onEventsMessage);
socket.on("eventToBroadcast",function() {
// broadcast event using pub/sub
// using apply to forward all arguments
eventsChannel.publish.apply(eventsChannel,arguments);
});
socket.on('disconnect', function(){
eventsChannel.removeListener("message",onEventsMessage);
});
}
}
To forward all incomming messages I also use the following trick:
socketio/socket.io#1715 (comment)
But, as you can see, this is not at all a drop in replacement.
It's probably better to improve socket.io-redis on the long run, by for example checking if we can make the "channels" namespace specific.
Actually... checking the source, you might be able to do this with the key option:
key: the name of the key to pub/sub events on as prefix (socket.io)
But I'm not sure if you can do this per namespace (instead of socket.io globally)
from socket.io-redis-adapter.
The same goes for the rooms, even if there is no client joined to them the socket.io server will still receive all the messages for that room from redis.
It is because socket.io-redis subscribes for all messages with the prefix (socket.io by default). With the current design it is needed, because messages are sent to channels prefix + '#' + clientId, no namespace or room in the channel name.
So messages from different namespaces will be received from redis.
The debug message says exactly this, socket.io-redis ignore different namespace
, so it gets discarded on the socket.io server, instead of simply not subscribing to them on redis.
sub.psubscribe(prefix + '#*', function(err){
if (err) self.emit('error', err);
});
You can test it with this repo.
https://github.com/koszta/socket.io-perf
In pull request #46 I changed the channel names to prefix + '#' + namespance + '#' [+ room + '#'], with this socket.io servers can subscribe to the necessary channels only.
from socket.io-redis-adapter.
This issue has been resolved in version 1.0.0
, which includes #46 as mentioned in the comment above.
from socket.io-redis-adapter.
Related Issues (20)
- Does this work with ioredis-mock? HOT 1
- redisClient.send_command is not a function HOT 1
- UnhandledPromiseRejection when trying to add a RedisAdapter to socketIO
- Consult about a logic problem in the code
- 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 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.