Shawn Varughese
Shawn Varughese

Reputation: 500

socket.io-redis keeps throwing NOAUTH error

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?

Upvotes: 0

Views: 5136

Answers (1)

h0x91B
h0x91B

Reputation: 1246

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
    }));
})

Upvotes: 0

Related Questions