download & runRedis Labs Enterprise Cluster beta

Learn more

free download

Connect & useRedis Cloud





By creating your account, you agree to our Terms of Use

Forgot your password?

Not Registered?

Forgot your password?

Enter your email address below to have your password reset. We'll send you an email with a link to change your password.

Your IP address will be logged and monitored for this request.

Cancel

Your password has reset!

We have sent you an email with a link to change your password.

OK
Blog

Using Redis with Node.js

In order to use Redis with Node.js you will need a Node.js Redis client. The examples in following section will demonstrate the use of node_redis, a complete Redis client for Node.js. Additional Node.js clients for Redis can be found under the Node.js section of the Redis Clients page.

Installing node_redis

node_redis installation instructions are given in the README file. To install using npm issue the following command:

$ npm install redis 

Opening a Connection to Redis Using node_redis

The following snippet demonstrates creating a connection to Redis using node_redis:

var redis = require('redis');
var client = redis.createClient(6379, 'hostname.redislabs.com', {no_ready_check: true});
client.auth('password', function (err) {
    if (err) then throw err;
});

client.on('connect', function() {
    console.log('Connected to Redis');
});

To adapt this example to your code, make sure that you replace the following values with those of your database:

Using SSL and node_redis

node_redis does not support SSL connections natively. For an added security measure, you can use stunnel to secure the connection or this node_redis fork that has been added with SSL support.

Reading and Writing Data with node_redis

Once connected to Redis, you can start writing and reading data. The following code snippet demonstrates writing the value bar to the Redis key foo and then reading it back:

// open a connection to Redis
...

client.set("foo", "bar", redis.print);
client.get("foo", function (err, reply) {
    if (err) then throw err;
    console.log(reply.toString());
});

The output of the above code should be:

$ node example_node_redis.js
Connected to Redis
bar

More Platforms

Interact with Redis Cloud

Enter your Redis commands,
see how fast it responds!

Redis Cloud > |