0

I am trying to connect to my redis server that password protected but for some reason I keep getting the error:

events.js:141 throw er; // Unhandled 'error' event ^ ReplyError: Ready check failed: NOAUTH Authentication required. at parseError (/home/ubuntu/TekIT/ITapp/node_modules/redis-parser/lib/parser.js:193:12) at parseType (/home/ubuntu/TekIT/ITapp/node_modules/redis-parser/lib/parser.js:303:14)

I know the password is correct because i tried it in the redis-cli and it works fine. Here is the code below:

var redis = require('redis');

var client = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });


var redisSubscriber = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });




// Create and use a Socket.IO Redis store
var RedisStore = require('socket.io-redis');
io.set('store', new RedisStore({
    redisPub: client,
    redisSub: redisSubscriber,
    redisClient: client
}));

Does anyone know why I am getting this error?

Shawn Varughese
  • 450
  • 4
  • 17

1 Answers1

0

You should initialize socket.io-redis after ready event.

Also you should call to client.auth('password1', ()=>{}) function.

Check this part of documentation:

When connecting to a Redis server that requires authentication, the AUTH command must be sent as the first command after connecting. This can be tricky to coordinate with reconnections, the ready check, etc. To make this easier, client.auth() stashes password and will send it after each connection, including reconnections. callback is invoked only once, after the response to the very first AUTH command sent. NOTE: Your call to client.auth() should not be inside the ready handler. If you are doing this wrong, client will emit an error that looks something like this Error: Ready check failed: ERR operation not permitted.

Try this code:

var redis = require('redis');
const redisPort = 6379
const redisHostname = 'localhost'
const password = 'password1'

var p1 = new Promise((resolve, reject)=>{
    var client = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });
    client.auth(password)
    client.on('ready', ()=>{
        resolve(client)
    })
})

var p2 = new Promise((resolve, reject)=>{
    var redisSubscriber = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });
    redisSubscriber.auth(password)
    redisSubscriber.on('ready', ()=>{
        resolve(redisSubscriber)
    })
})

Promise.all([p1,p2]).then(([client, redisSubscriber])=>{
    console.log('done', client)
    client.ping((err, data)=>{
        console.log('err, data', err, data)
    })

    // Create and use a Socket.IO Redis store
    var RedisStore = require('socket.io-redis');
    io.set('store', new RedisStore({
        redisPub: client,
        redisSub: redisSubscriber,
        redisClient: client
    }));
})
h0x91B
  • 1,211
  • 15
  • 19
  • ok so how would i initialize socket.io-redis after a ready event? Is this how I should do it? var redisSubscriber = redis.createClient(redisPort, redisHostname); redisSubscriber.auth('password1', function (err) { if (err) throw err; else { // Create and use a Socket.IO Redis store RedisStore = require('socket.io-redis'); io.set('store', new RedisStore({ redis: redis, redisPub: client, redisSub: redisSubscriber, redisClient: client })); } }); – Shawn Varughese Apr 27 '17 at 12:10
  • Can you give me an example of how to do it properly, please? – Shawn Varughese Apr 27 '17 at 12:10
  • or do you mean like this: client.on('ready', function () { redisSubscriber.on('ready', function () { console.log("Everything Ready!"); // Create and use a Socket.IO Redis store RedisStore = require('socket.io-redis'); io.set('store', new RedisStore({ redis: redis, redisPub: client, redisSub: redisSubscriber, redisClient: client })); }) }) – Shawn Varughese Apr 27 '17 at 13:35
  • one minute please, I'll make it – h0x91B Apr 27 '17 at 13:36
  • That worked!!! Thanks so much I was driving myself nuts driving to figure it out! – Shawn Varughese Apr 27 '17 at 18:07