socket.io-redis giving error on heroku

782 views Asked by At

i am trying to deploy my app on heroku.i added redistogo addon in my app.it is based on node.js and express.

i write a small code

var redis = require('socket.io-redis');

var io = require('socket.io')(server);
io.adapter(redis(process.env.REDISTOGO_URL));

but on last line i am getting error:

Error: Redis connection to redistogo:6379 failed - getaddrinfo ENOTFOUND redistogo

can any one help why i am facing this error and get rid of this error.6379 is default port but my redistogo url doesn't has 6379 port no.it's port no is 10281.

Is this a bug in socket.io-redis module or i am doing something wrong ??

2

There are 2 answers

1
mathlearner On

If redis DB has a password then it's better to opt for

 var redis = require('redis').createClient;
    var adapter = require('socket.io-redis');
    var pub = redis(port, host, { auth_pass: "pwd" });
    var sub = redis(port, host, { detect_buffers: true, auth_pass: "pwd" 

});
    io.adapter(adapter({ pubClient: pub, subClient: sub }));

in case of heroku enter host as

redis://redistogo:[email protected]
and port : provided in redistogo_url

and now it's working great.

1
galethil On

If your Redis is running on port 10281 you need to set it when initializing adapter.

var io = require('socket.io')(3000); var redis = require('socket.io-redis'); io.adapter(redis({ host: process.env.REDISTOGO_URL, port: 10281 }));

Check out documentation https://github.com/automattic/socket.io-redis#adapteropts