Nodejs Redis Client

Lets us first install the nodejs Redis Client by the following command

$ npm install redis

Before that you need to install the Redis Server as mentioned in the earlier post. Now lets create redis.js client file to interact with redis server.

var redis = require('redis'); //creating redis client var client = redis.createClient();
//set the strings with values client.set("string1", "Hello World of string1");
client.set("string2", "Hello World of string2");
//get the string values client.get("string1", function(err, reply)
{ console.log(reply); }); client.get("string2", function(err, reply)
{ console.log(reply); });

Save the above code as redis1.js and execute it by

$ node redis1.js

Now lets deal with message list pushing and push

var redis = require('redis'); //creating redis client var client = redis.createClient();
//push string1 and string2 to strings var string1 = "Hello World of string1";
client.lpush("strings", string1, function(err, reply){
console.log(reply);
}); var string2 = "Hello World of string2";
client.lpush("strings", string2, function(err, reply){
console.log(reply);
}); //extract all strings values; client.lrange("strings", 0, -1, function(err, messages){
console.log(messages);
}); //pop strings client.lpop("strings", function(err, messages){
console.log(messages);
});

Save the above code as redis2.js and execute it by

$ node redis2.js