Our application is beginning to take shape. We have a list of projects and a form that allows us to add, delete, and update projects. We are also able to assign repositories to these projects, which allows us to view a list of issues/commits for all repositories in a project. This chapter will guide you through the next phase of our client setup: displaying a list of project repository commits and issues in real time using Redis and Socket.IO.
We would ideally like the application to continue working with Socket.IO/Redis switched off, leaving the application without a real-time element. We will attempt to implement these features with this in mind.
Redis is an extremely fast, open source, in-memory key value store. Redis has a useful Pub/Sub mechanism that we will use to push messages to a Socket.IO subscriber that will emit events to the client.
Visit this website in order to download and install Redis: http://redis.io/download.
Once Redis is installed, you can start it with the following command:
redis-server
In order to start the Redis command-line interface, CLI issues the following command:
redis-cli
The following commands can be issued from the CLI:
monitor
flushall
keys *
get <key>
In order to use Redis in our application, install the node-redis
client, as follows:
npm install redis --save
Let's configure our application to use Redis by updating the./lib/config/*.json
config files with the following configuration:
"redis": { "port": 6379 , "host": "localhost" }
First, we create a simple module, Redis
, that wraps up the Redis connection ./lib/cache/redis.js
. We start by importing the redis
module. We define a Redis
module, which calls createClient
in order to create a Redis client.
We pull in the Redis configuration data from the preceding:
var redis = require('redis') , config = require('../configuration'), function Redis() { this.port = config.get("redis:port"); this.host = config.get("redis:host"); this.password = config.get("redis:password"); this.client = redis.createClient(this.port, this.host); if (this.password) this.client.auth(this.password, function() {}); } module.exports = Redis;
Let's extend our Redis
module and create a Publisher
module that will publish messages using the Redis Pub/Sub feature, ./lib/cache/publisher/index.js
. We start by importing our Redis
module and use the util
module to extend the Redis
module with the Publisher
module. We then define our Publisher
module, which includes a save
function, which saves an object as a string to Redis and a publish
function, which publishes a message to Redis.
The Publisher
module is defined as shown in the following code snippet:
var Redis = require('../../cache/redis') , util = require('util'), util.inherits(Publisher, Redis); function Publisher() { Redis.apply(this, arguments); }; Redis.prototype.save = function(key, items) { this.client.set(key, JSON.stringify(items)); }; Redis.prototype.publish = function(key, items) { this.client.publish(key, JSON.stringify(items)); }; module.exports = Publisher;
Next, we extend our Redis
module and create a Subscribe
r./lib/cache/subscriber/index.js
, which consumes published messages. We start by importing our Redis
module and use the util
module to extend the Redis
module with the Subscriber
module. We then define our Subscriber
module, which includes a subscribe
function. This allows the user to subscribe to messages on a key
:
var Redis = require('../../cache/redis') , util = require('util'), util.inherits(Subscriber, Redis); function Subscriber() { Redis.apply(this, arguments); }; Subscriber.prototype.subscribe = function(key) { this.client.subscribe(key); }; module.exports = Subscriber;
3.149.28.145