Simple Using Node js and angular js

Simple Using Node js and angular js

To see the working demo open following link in two different client (Browser).

Demo Link

Setting up the Project

First we need to create a project, do this with the following command

npm init

Installing Back-End Dependencies

Create a server folder inside the root directory of the app and inside the folder, create a package.json file, adding the following:

  "name": "simplSocketChatApp",
  "version": "1.0.0",
  "description": "",
  "scripts": {
  "start": "node server.js",
   "test": "echo \"Error: no test specified\" && exit 1"
  "author": "Avdhesh(ej)",
  "license": "ISC",
  "dependencies": {
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "": "^2.0.4"

Save the file and execute npm install. This installs the all dependencies. Server

Create a server.js file and add the following code:

var express = require('express');
var path = require('path');
var app = express();
var server = require('http').createServer(app);
app.use('/', express.static(path.join(__dirname, '/UI')));
var io = require('')(server);
io.on('connection', function(client) {
    client.on('disconnect', function() {
    client.on('room', function(data) {
        console.log(' Client joined the room and client id is '+;

    client.on('toBackEnd', function(data) {
     'message', data);

Installing Front-End Dependencies

Create a bower.json file in the UI directory of the project and add the following code:

  "name": "simplchat",
  "description": "A simple socket app",
  "authors": [
  "license": "ISC",
  "ignore": [
  "dependencies": {
    "": "sio-client#^1.4.8",
    "angular-socket-io": "^0.7.0",
    "angular-local-storage": "^0.7.1"
  • angular-local-storage: Used for storing data in local storage. In the following project it’s used to store the name of the user, and the current room.
  • sio-client: The JavaScript client.
  • angular-socket-io: Allows us to use within Angular.

We are using angular material for DOM.


Directory: project_folder/UI/

Start by opening the index.html file inside the UI directory and linking the script files.

<html lang="en">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">

<body ng-app="simpleSocketApp" ng-cloak>

    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>    <script src=""></script>
    <script src="lib/angular-local-storage/dist/angular-local-storage.min.js"></script>
    <script src="lib/"></script>
    <script src="http://localhost:3000/"></script>
    <script src="lib/angular-socket-io/socket.min.js"></script>
    <script src="js/app.js"></script>
    <div ng-include="'view/home.html'"></div>


Now we are going to create simple view for the sending messages.

<div ng-controller="homeController">
        <label>Add message</label>
        <input ng-model="message.input">
    <md-button ng-click="add()" class="md-warn">Send</md-button>

    <md-toolbar layout="row" class="md-hue-3">
        <div class="md-toolbar-tools">

        <md-list flex>
            <md-subheader class="md-no-sticky">Updated List</md-subheader>
            <md-list-item class="md-3-line" ng-repeat="item in array" ng-click="null">

                <div class="md-list-item-text" layout="column">
                    <h3>{{ }}</h3>
                    <h3>{{ | date:'medium'}}</h3>


Adding the Dependencies

Open the /js/app.js file and include the following services

  • LocalStorageModule: Allows us to use local storage.
  • btford.socket-io: Allows us to use

Add following lines in the app.js

var app = angular.module('MySocektApp', ['ngMaterial', 'LocalStorageModule', 'btford.socket-io']);

app.service('SocketService', ['socketFactory', function SocketService(socketFactory) {
    return socketFactory({
        ioSocket: io.connect('http://localhost:3000')

app.controller('homeController', function($scope, localStorageService, SocketService) {

    $scope.array = [];
    $scope.message = {};
    SocketService.emit('room', { roomId: "temp" });

    $scope.add = function() {
        SocketService.emit('toBackEnd', {roomId:'temp', data: $scope.message, date: new Date() })
        $scope.array.push({ data: $scope.message, date: new Date() })

    SocketService.on('message', function(msg) {



In above tutorial we have learned how to create a socket application with Angular js and node js . This application is basic idea of featured nodejs but we can use this idea ine following area:

  • Angular powered cross chat app
  • A real time video calling module using