Private Chat Application with Node.js, Socket.IO and AngularJS

Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and servers. On the another hand we have Node.js a JavaScript runtime built on Chrome’s V8 JavaScript engine. Here, We are going to create a Private Chat Application with Node.js, Socket.IO and AngularJS, Where I used AngularJS on my front-end to handle my server request/services.

Server-Side Program :

var express = require('express');

var app = express();
app.set('port', process.env.PORT || 9000);
var server = require('http').Server(app);
var io = require('')(server);
var port = app.get('port');


server.listen(port, function () {
    console.log("Server listening on: http://localhost:%s", port);

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');

var usernames = {};
var rooms = [];

io.sockets.on('connection', function (socket) {
    socket.on('adduser', function (data) {
        var username = data.username;
        var room =;

        if (rooms.indexOf(room) != -1) {
            socket.username = username;
   = room;
            usernames[username] = username;
            socket.emit('updatechat', 'SERVER', 'You are connected. Start chatting');
  'updatechat', 'SERVER', username + ' has connected to this room');
        } else {
            socket.emit('updatechat', 'SERVER', 'Please enter valid code.');
    socket.on('createroom', function (data) {
        var new_room = ("" + Math.random()).substring(2, 7);
        rooms.push(new_room); = new_room;
        socket.emit('updatechat', 'SERVER', 'Your room is ready, invite someone using this ID:' + new_room);
        socket.emit('roomcreated', data);

    socket.on('sendchat', function (data) {'updatechat', socket.username, data);

    socket.on('disconnect', function () {
        delete usernames[socket.username];
        io.sockets.emit('updateusers', usernames);
        if (socket.username !== undefined) {
            socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');

Client-Side Program :

var app = angular.module('myApp', []);

/* Controllers */
app.controller('AppCtrl', function ($scope, socket) {

  $scope.users = [];
  $scope.curtrentUser = '';
  socket.on('connect', function () { });

  socket.on('updatechat', function (username, data) {
    var user = {};
    user.username = username;
    user.message = data; = new Date().getTime();
    user.image = '' + username.charAt(0).toUpperCase();

  socket.on('roomcreated', function (data) {
    socket.emit('adduser', data);

  $scope.createRoom = function (data) {
    $scope.curtrentUser = data.username;
    socket.emit('createroom', data);

  $scope.joinRoom = function (data) {
    $scope.curtrentUser = data.username;
    socket.emit('adduser', data);

  $scope.doPost = function (message) {
    socket.emit('sendchat', message);

/* Services */
app.factory('socket', function ($rootScope) {
  var socket = io.connect();
  return {
    on: function (eventName, callback) {
      socket.on(eventName, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
    emit: function (eventName, data, callback) {
      socket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if (callback) {
            callback.apply(socket, args);

You can download the source code via GitHub. Downloaded source code can be run/executed with the following commands,

npm install #Install packages dependencies
node app    #Start the Server/Application

Please leave your valuable comments/suggestions/feedback on below comment box if any.

Thank You !!

Siva Sankar

SIVA SANKAR, Working as a Software Engineer, Blogging is my hobby. I completed my Bachelors of Engineering (Computer Science Engineering) in Chennai, India. and my Master of Engineering (Embedded System Technologies) in Tamilnadu, India.

28 thoughts on “Private Chat Application with Node.js, Socket.IO and AngularJS

  1. Hi,
    How can I do one to one chat with existing users. Like users already stored in database. We show list of users. I can then select any of the user and start chatting with him privately.

      1. hey siva, can you plzz elaborate this point with an example. how do we get the socket id of other user from room and send msg to it.

  2. Good, Creating a simple chat application, then the following development stack works very nicely together:
    Express.js (Node.js)

  3. Hello Sir, Chat app is excellent. One more thing, I want to know how we can setup database connectivity so that we can save our messages to database. Any help please Thanks in advance.

    Thanks for app 🙂

  4. Hi ,

    please help me,I need to create a private chat application and but i did not find any sucess .

    please help me if any sample of demo code if you provide me its useful for me .


Leave a Reply

Your email address will not be published. Required fields are marked *