{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

5-Minute Beginner's Guide: Installing / Testing Redis and Node.js on OS X

Published by

Redis is an open source, advanced key-value store. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Today I’m going to show you how quickly you can get Redis and Node.js running on OS X. In addition, I’ll show you two quick demos you can then try out with Node.js.

Note: I’m using OS X version 10.7.3 and I have the latest XCode installed (available on the App Store). Much of this information is available at the respective websites, I just wanted to put it all in one place for beginners.

Step one, go grab the OS X package from the Nodejs website and download and install it. The package is pre-built and is very simple to install.

Step Two: Once the above is done go to the Redis website and download the latest Redis package. Once the tar file was downloaded I double clicked it to extract it, you can also issue the follow command line from the terminal:

$ tar xzf redis-2.x.x.tar.gz

From the terminal go into the new folder that was created when you extracted Redis:

$ cd redis-2.x.x

Still within the Redis folder simply run the make command now:

$ make

Test everything out:

$ make test

Time to start Redis:

$ src/redis-server

Let’s start with a very simple program. The program will simply set a value and then fetch it back.

First, create a new folder and inside that folder create a new text file called app.js. Inside the app.js file add the following code:

var redis = require("redis"),
    client = redis.createClient();
client.on("error", function (err) {
    console.log("Error " + err);
// Set a value
client.set("string key", "Hello World", redis.print);
// Get the value back
client.get("string key", function (err, reply) {
// Clean quit (waits for client to finish)

Now, let’s install node_redis locally via NPM:

Go back into the terminal and navigate the folder where you placed the app.js file and enter this command from within that folder:

$ npm install redis

Note: You can also install a non-blocking C library version like this:

$ npm install hiredis redis

Run the Node.js program like so:

$ node app.js

Output will look similar to this:

Reply: OK
Hello World

Lets build a more complex program now. Modify the app.js file and replace the previous code with the code below (taken from the node_redis project on Github):

var redis = require("redis"),
    client = redis.createClient();
client.on("error", function (err) {
    console.log("Error " + err);
client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);

Run the Node.js program now like so:

$ node app.js

Output will look similar to this:

Reply: OK
Reply: 1
Reply: 1
2 replies:
    0: hashtest 1
    1: hashtest 2

In the terminal and the Redis folder you can safely shutdown Redis with the following command:

$ src/redis-cli shutdown

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB.

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks