Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to RedisToGo through Node.JS

Tags:

node.js

redis

I'm using Redis To Go in combination with the https://github.com/mranney/node_redis library. Redis gives me a url that looks like redis://me:[email protected]:9393 but I don't know how to use it as createClient() only takes the host and the port.

like image 278
scratch Avatar asked Sep 02 '11 23:09

scratch


2 Answers

I believe that the scheme for the URL you have is:

redis://username:password@host:port.

I don't believe username is used. node_redis provides two methods that you'll use to log in: createClient and auth. There are details in the readme, but for reference here is the relevant portion:

redis.createClient(port, host, options)

Create a new client connection. port defaults to 6379 and host defaults to 127.0.0.1. If you have redis-server running on the same computer as node, then the defaults for port and host are probably fine. options in an object with the following possible properties:

  • parser: which Redis protocol reply parser to use. Defaults to hiredis if that module is installed. This may also be set to javascript.
  • return_buffers: defaults to false. If set to true, then bulk data replies will be returned as node Buffer objects instead of JavaScript Strings.

createClient() returns a RedisClient object that is named client in all of the examples here.

client.auth(password, callback)

When connecting to Redis servers that require 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.

like image 142
Michelle Tilley Avatar answered Sep 30 '22 00:09

Michelle Tilley


I also had to add the parameter no_ready_check: true to the call to redis.createClient().

client = redis.createClient(settings.redis.port, 
                            settings.redis.host, 
                            {no_ready_check: true});
if (settings.redis.password) {
  client.auth(settings.redis.password, function() {
    console.log('Redis client connected');
  });
}
like image 40
daveh Avatar answered Sep 30 '22 02:09

daveh