Coder Social home page Coder Social logo

Comments (21)

nkzawa avatar nkzawa commented on May 17, 2024 5

You can access to an adapter instance like:

io.of('/').adapter.on('error', funtion(err) {});

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024 2

Putting a listener on adapter.prototype actually works... That's far from intuitive...
Example:

var adapter = socketIORedis();
adapter.prototype.on('error',function(err) {
  debug('adapter error: ',err);
});
io.adapter(adapter);

from socket.io-redis-adapter.

rauchg avatar rauchg commented on May 17, 2024

The adapter inherits from Emitter, so simply attach an error listener on the emitter instance.

from socket.io-redis-adapter.

yaroshevych avatar yaroshevych commented on May 17, 2024

@guille, could you please advise on valid syntax for 'error' event. I can't find any working solution.

from socket.io-redis-adapter.

rauchg avatar rauchg commented on May 17, 2024

Attach it on the adapter instance? On the object you pass to io.adapter

from socket.io-redis-adapter.

yaroshevych avatar yaroshevych commented on May 17, 2024

Guillermo, thanks for your feedback, unfortunately it doesn't work with v. 0.1.3. Here is my code:

var redis = require('socket.io-redis');
var redisAdapter = redis({ host: 'abc', port: 12345 });
redisAdapter.on('error', function(err) {});
io.adapter(redisAdapter);

There is no 'on' method in redisAdapter object. Here is error:

   redisAdapter.on('error', function(err) {});
                 ^
TypeError: Object function Redis(nsp){
    Adapter.call(this, nsp);

    var self = this;
    sub.psubscribe(prefix + '#*', function(err){
      if (err) self.emit('error', err);
    });
    sub.on('pmessage', this.onmessage.bind(this));
  } has no method 'on'

from socket.io-redis-adapter.

mathiask88 avatar mathiask88 commented on May 17, 2024

Try redisAdapter.prototype.on('error', function(err) {}); that worked for me. But it feels a bit strange to edit the prototype...

from socket.io-redis-adapter.

yaroshevych avatar yaroshevych commented on May 17, 2024

It's not working for me. When I kill Redis server, whole app crashes:

events.js:72
        throw er; // Unhandled 'error' event

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

I'm having the same issue, please reopen.
The emit in the psubscribe callback is throwing an error, which can't be catched in a good way.
https://github.com/Automattic/socket.io-redis/blob/master/index.js#L73
I did add an error event handler:

var adapter = io.adapter(socketIORedis({  key: 'socket.io-redis-experiment',
                            pubClient: pub,
                            subClient: sub}));
adapter.on('error',function(err) {
  debug('adapter error: ',err);
});

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

Adding a error listener on self, just before sub.psubscribe solves the unhandled error...

self.on('error',function(err) {
  console.log("socket.io-redis inside error handler: ",err);
});

Maybe the right instance reference isn't returned from the require?

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

I think I understand the problem, require('socket.io-redis') returns a "class" and every namespace creates it's own adapter instance from this.

from socket.io-redis-adapter.

emidiocroci avatar emidiocroci commented on May 17, 2024

I tried the solution suggested by @peteruithoven but no luck... The error is still thrown causing a crash of the application. Is there some news about this issue?

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

@emidiocroci, did you also try adding error listeners to the pub and sub clients?

from socket.io-redis-adapter.

emidiocroci avatar emidiocroci commented on May 17, 2024

Not... How can I do it?
Il 23/ott/2014 23:20 "Peter Uithoven" [email protected] ha scritto:

@emidiocroci https://github.com/emidiocroci, did you also try adding
error listeners to the pub and sub clients?


Reply to this email directly or view it on GitHub
#21 (comment)
.

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

Small example:

io.adapter(initRedisAdapter(config.REDIS_PORT,config.REDIS_HOST));
function initRedisAdapter(port,host) {
  var pub = redis.createClient(port,host,{detect_buffers: true});
  pub.on('error',onRedisError);
  var sub = redis.createClient(port,host,{detect_buffers: true});
  sub.on('error',onRedisError);
  var redisAdapter = RedisAdapter({pubClient: pub,
                            subClient: sub,
                            key: 'your key'});
  redisAdapter.prototype.on('error',onRedisError);
  function onRedisError(err){
    debug("Redis error: ",err);
  }
  return redisAdapter;
}

from socket.io-redis-adapter.

emidiocroci avatar emidiocroci commented on May 17, 2024

Thanks @peteruithoven, worked like a charm!
Where did you find those info about redis adapter initialization?

from socket.io-redis-adapter.

peteruithoven avatar peteruithoven commented on May 17, 2024

Source code of socket.io ;)

from socket.io-redis-adapter.

pkuhonker avatar pkuhonker commented on May 17, 2024

@nkzawa 3x! You are right. Using radis adapter API also need to add of(namespace)!!!

from socket.io-redis-adapter.

pherrymason avatar pherrymason commented on May 17, 2024

io.of('/').adapter.on('error', funtion(err) {}); this didn't work for me,
only adding the handler to the prototype as @peteruithoven suggesed worked for me

from socket.io-redis-adapter.

edevil avatar edevil commented on May 17, 2024

Same. Using version 5.2.0 and only catching errors on the pubClient and subClient still caused my app to crash.

Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)

Adding:

  adapter.prototype.on("error", err => logger.error("PROTOTYPE", { err }));

will prevent the app from crashing:

2018-10-25T10:17:56.307Z error: PROTOTYPE [err: Error: connect ECONNREFUSED 127.0.0.1:6379] [severity: ERROR]
2018-10-25T10:17:56.308Z error: PROTOTYPE [err: Error: connect ECONNREFUSED 127.0.0.1:6379] [severity: ERROR]
2018-10-25T10:17:56.467Z error: PROTOTYPE [err: MaxRetriesPerRequestError: Reached the max retries per request limit (which is 3). Refer to "maxRetriesPerRequest" option for details.] [severity: ERROR]

However, it will not retry forever to establish the Redis connection and will stay zombie.

from socket.io-redis-adapter.

salmanahmad94 avatar salmanahmad94 commented on May 17, 2024

Same. Using version 5.2.0 and only catching errors on the pubClient and subClient still caused my app to crash.

Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)

Adding:

  adapter.prototype.on("error", err => logger.error("PROTOTYPE", { err }));

will prevent the app from crashing:

2018-10-25T10:17:56.307Z error: PROTOTYPE [err: Error: connect ECONNREFUSED 127.0.0.1:6379] [severity: ERROR]
2018-10-25T10:17:56.308Z error: PROTOTYPE [err: Error: connect ECONNREFUSED 127.0.0.1:6379] [severity: ERROR]
2018-10-25T10:17:56.467Z error: PROTOTYPE [err: MaxRetriesPerRequestError: Reached the max retries per request limit (which is 3). Refer to "maxRetriesPerRequest" option for details.] [severity: ERROR]

However, it will not retry forever to establish the Redis connection and will stay zombie.

To keep the reconnection protocol, instantiate the redis client separately from the adapter. Implement reconnection protocols on this redis client and let it reconnect. Let adapter.prototype.on catch the error handle even redundantly. That way, you'll get the best of both worlds.

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.