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

Make Node.js Redis super easy with Redis Labs

Redis Labs' products and services enable running Redis datasets in a highly available and auto-scalable manner, with predictable top performance.

The Redis Labs Enterprise Cluster (RLEC) lets you install an enterprise grade Redis cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration - no clustering or sharding knowledge required!

Redis Cloud is a fully-managed cloud service for hosting and running Redis dataset without dealing with nodes, clusters, failure recovery or performance stabilization. Our technology does all that in a fully automated manner. Redis Cloud is available on all popular clouds and platforms-as-a-service.

Using Redis with Node.js

In order to use Redis with Node.js you will need a Node.js Redis client. In following sections, we 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 code creates a connection to Redis using node_redis:

var redis = require('redis');
var client = redis.createClient(port, 'hostname', {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 secure the connection using stunnel 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 reading and writing data. The following code snippet writes the value bar to the Redis key foo, reads it back, and prints it:

// 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

For more information on using Redis Labs' products and services with Node.js please see the Howto page.

More Platforms

Interact with Redis Cloud

Enter your Redis commands,
see how fast it responds!

Redis Cloud > |

See Our Open Source Contributions