Giter Site home page Giter Site logo

Comments (6)

peteruithoven avatar peteruithoven commented on May 22, 2024

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.

RWOverdijk avatar RWOverdijk commented on May 22, 2024

👍

from socket.io-redis-adapter.

mguihal avatar mguihal commented on May 22, 2024

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.

peteruithoven avatar peteruithoven commented on May 22, 2024

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.

koszta avatar koszta commented on May 22, 2024

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.

objectiveSee avatar objectiveSee commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.