Simple Chat Application Using Redis,, and Node.js

Install Redis on Ubuntu

Once the program has been installed, Redis comes with a built in script that sets up Redis to run as a background daemon. To access the script move into the utils directory:

  • cd utils
  • sudo ./

As the script runs, you can choose the default options by pressing enter. Once the script completes, the redis-server will be running in the background. You can start and stop redis with these commands (the number depends on the port you set during the installation. 6379 is the default port setting):

  • sudo service redis_6379 start
  • sudo service redis_6379 stop

You can then access the redis database by typing the following command:

  • redis-cli

You now have Redis installed and running. The prompt will look like this:

  • redis

To set Redis to automatically start at boot, run:

  • sudo update-rc.d redis_6379 defaults

To check detail information for your installation:

  • redis-cli
  • info

Make sure you have the latest version installed!

Creating a simple chat application:
Server Side Code (app.js):

var app = require('http').createServer(handler);
var io = require('').listen(app);
var redis = require('redis');
var fs = require('fs');

function handler(req,res){
    fs.readFile(__dirname + '/index.html', function(err,data){
            return res.end('Error loading index.html');
        console.log("Listening on port 8088");

var store = redis.createClient();
var pub = redis.createClient();
var sub = redis.createClient();

io.sockets.on('connection', function (client) {
    sub.on("message", function (channel, message) {
        console.log("message received on server from publish ");
    client.on("message", function (msg) {
        if(msg.type == "chat"){
        else if(msg.type == "setUsername"){
            pub.publish("chatting","A new user in connected:" + msg.user);
    client.on('disconnect', function () {
        pub.publish("chatting","User is disconnected :" +;

The index.html file:

    <title>Socket and Redis in Node.js</title>
    <script src="/"></script>
    <script src=""></script>
<div id="username">
    <input type="text" name="usernameTxt" /> <input type="button" name="setUsername" value="Set Username" />
<div id="sendChat" style="display:none;">
    <input type="text" name="chatTxt" /> <input type="button" name="sendBtn" value="Send" />
<br />
<div id="content"></div>
    $(document).ready(function() {
        var username = "anonymous";
	        if($('input[name=usernameTxt]').val() != ""){
	            username = $('input[name=usernameTxt]').val();
	            var msg = {type:'setUsername',user:username};
        var socket = new io.connect('http://localhost');
        var content = $('#content');

        socket.on('connect', function() {

        socket.on('message', function(message){
            content.append(message + '<br />');
        }) ;

        socket.on('disconnect', function() {

            var msg = {type:'chat',message:username + " : " +  $("input[name=chatTxt]").val()}

To run this application, you must install node sever, redis, and
After that, simply run: node app.js
Then open your browser and navigate http://localhost:8088

This entry was posted in open-source. Bookmark the permalink.

1 Response to Simple Chat Application Using Redis,, and Node.js

  1. banckle says:

    Thanks for providing wonderful information I would suggest you to check Banckle Chat ( ) which is one of the best live chat application for your website having lots of built-in featuers.

    Signup FREE:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s