Private Chat Messaging using Node.js,, Redis in PHP


I am working for a real time private messaging system into my php application. My codes are working for all users together. But I need private messaging system as one-to-one message.

After setup node and redis I can get the message data what I need : here is the code ::

Front-end ::
1. I use a form for – username , message and send button

and Notification JS:

$( document ).ready(function() {

    var socket = io.connect('');

    socket.on('notification', function (data) {
        var message = JSON.parse(data);
        $( "#notifications" ).prepend("<p> <strong> " + message.user_name + "</strong>: " + message.message + "</p>" );



Server Side js:

var app = require('express')();
var server = require('http').Server(app);
var io = require('')(server);
var redis = require('redis');


var users = {};
var sockets = {};

io.on('connection', function (socket) {

    console.log(" New User Connected ");
    // instance of Redis Client
   var redisClient = redis.createClient();

   socket.on('set nickname', function (name) {
       socket.set('nickname', name, function () {

  socket.on('msg', function () {
    socket.get('nickname', function (err, name) {
        console.log('Chat message by ', name);

   redisClient.on("message", function(channel, message)
     // to view into terminal for monitoring
     console.log("Message from: " + message + ". In channel: " + channel + ". Socket ID "+ );

     //send to socket
     socket.emit(channel, message);

   redisClient.on('update_chatter_count', function(data)
      socket.emit('count_chatters', data);

   //close redis
   socket.on('disconnect', function()



<script src=""></script>
<form .....>
<input ....... >
<div id="notifications" ></div>

Over-all output:

John : Hello
Kate : Hi
Others: .....

Above codes are working nicely in my php application. Now I want to set-up private or one-to-one messaging system.

The way I need to add username or email or unique socketID for user. I do not have any more ideas for private messaging. I tried to figure on online but failed.

**How do I setup private message into my php application ? **


Basic initialization of variables:-

First, make a MAP of mapOfSocketIdToSocket and then send the userid of the specific user to whom you want to sent the message from the front-end. In the server, find the socket obeject mapped with the userid and emit your message in that socket. Here is a sample of the idea (not the full code)

  var io = socketio.listen(server);
  var connectedCount = 0;
  var clients = [];
  var socketList = [];
  var socketInfo = {};
  var mapOfSocketIdToSocket={};

  socket.on('connectionInitiation', function (user) {
      io.sockets.sockets['socketID'] =;
      socketInfo = {};
      socketInfo['connectTime'] = new Date();
      socketInfo['socketId'] =;

      socket.nickname =;
      socket.userId= user.userId;

      loggjs.debug("<"+ + "> is just connected!!");

  socket.on('messageFromClient', function (cMessageObj, callback) {
     for(var i=0; i<socketList.length;i++){
       if(socketList[i]['userId']==cMessageObj['messageToUserID']){ // if user is online
        mapOfSocketIdToSocket[socketList[i]['socketId']].emit('clientToClientMessage', {sMessageObj: cMessageObj});

Either you may want to go for private one-to-many chat room or you can go for one-to-one channel communication (if there are only two members communicating)

Answered By – sharif2008

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published