Click here to Skip to main content
15,881,248 members
Articles / Web Development / HTML5

HTML5 Chat with AngularJs, Spike Engine and Twitter Bootstrap

Rate me:
Please Sign up or sign in to vote.
4.91/5 (47 votes)
23 Jun 2015CPOL3 min read 133.2K   6.6K   102   24
A modern real-time HTML5 chat implementation for the web and mobile worlds.

Introduction

Everything in the internet becomes increasingly real-time and HTML5 finally offers some facilities to build efficient, simple and robust real-time applications on the web. This article demonstrates how an HTML5 chat can be built using Google's AngularJS framework for front-end rendering.

  1. It uses HTML5 Data URI for rendering images streamed from the server as byte arrays and identicons for avatars.
  2. It uses CSS3 for styling and JQuery for animations.
  3. It uses custom fonts and Google's AngularJS for client-side.
  4. It is cross-platform and with a minimized packet payload and message compression.
  5. The application server is a self-hosted executable and the client is just a plain html file.

[Live Demo]

Server-Side Implementation

This article is a continuation and an improvement upon our previous article demonstrating a jquery chat box written using Spike Engine as a back-end. In this article, we imrove the client-side considerable without making any significant changes to the server implementation.

Server-side implementation is described in detail in the previous article, please read it if you haven't read it prior to this article.

Client-Side Implementation

AngularJS is a MVC framework, rendered by your browser. One of the things it allows us to do, is to improve on our previous JQuery implementation by removing all the code that creates the DOM parts and appends them. Instead, AngularJS allows us to simply data-bind the data we are getting from the server.

In the figure above you can see how AngularJS data binding works. Because the view is just a projection of the model, the controller is completely separated from the view and unaware of it.

To make the chat, we first need to implement our template that does the actual rendering, to do so we create a NgChatCtrl that will be our controller and add to the scope few fields, we need one for messages and one for the messageText to be entered and sent to the client.

Additionally in the controller, we have two out of scope variables for our server and the flipping side of the chat bubble. One important thing to notice is that in the code below, the IP Address is pointing to 127.0.0.1, which means only local browser will be able to access the server. In the case where the server goes into production, this address should actually be the public IP / hostname.

JavaScript
function NgChatCtrl($scope) {
    // Our server to connect to
    var server = new spike.ServerChannel('http://127.0.0.1:8002');
    var side = 'left';

    // Messages, client info & sending
    $scope.messages = [];
    $scope.sendMessage = function () {
        server.sendNgChatMessage($scope.messageText);
        $scope.messageText = "";
    };

    // Occurs when we receive chat messages
    server.on('ngChatMessagesInform', function (p) {
        $scope.messages.push({
            avatar: "data:image/png;base64," + p.avatar.toBase64(),
            text: p.message,
            side: side
        });
        $scope.$apply();

        // Animate
        $("#viewport-content").animate({
            bottom: $("#viewport-content").height() - $("#viewport").height()
        }, 250);

        // flip the side
        side = side == 'left' ? 'right' : 'left';
    });

    // Once connected, we need to join the chat
    server.on('connect', function () {
        server.joinNgChat();
    });
}

As you can see above, we hook our ngChatMessagesInform event and simply push a new object for every message representing the chat "row" we need to show. We also flip the side, as we want to alternate the avatar being on the left or on the right of the bubble.

Our template is relatively simple. One thing to notice is that we have ng-repeat that will automatilly repeat the <div> inside our DOM. We also data-bind classes for avatar alternation and we bind the avatar and text properties.

The last thing we had to do is to bind the messageText property to the text box and allow us to actually send messages to the server.

HTML
<div id="browser-window" ng-app ng-controller="NgChatCtrl">
    <div id="viewport">
        <div id="viewport-content">
            <div class="bubble-container" ng-repeat="m in messages">
                <div class="avatar avatar-{{m.side}}"><img src="{{m.avatar}}" /></div>
                <div class="bubble bubble-{{m.side}}">{{m.text}}</div>
            </div>
        </div>
    </div>

    <form class="row row-chat" ng-submit="sendMessage()">
        <div class="input-group">
            <input type="text" class="form-control" ng-model="messageText" placeholder="Type your message" />

            <span class="input-group-btn">
                <button type="submit" class="btn btn-primary">Send</button>
            </span>
        </div>

    </form>
</div>

 

History

  • 23/06/2015 - Source code & article updated to Spike v3
  • 12/07/2014 - Deployment Explanation
  • 20/06/2014 - Initial Version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants, Ph.D. is the architect behind emitter.io service, a real-time, low-latency publish/subscribe service for IoT, Gaming. He is a software engineer and scientist with extensive experience in different computer science domains, programming languages/principles/patterns & frameworks.

His main expertise consists of C# and .NET platform, game technologies, cloud, human-computer interaction, big data and artificial intelligence. He has an extensive programming knowledge and R&D expertise.



Comments and Discussions

 
QuestionChatbot Pin
Member 1414886212-Feb-19 22:07
Member 1414886212-Feb-19 22:07 
GeneralMy vote of 5 Pin
D V L12-Sep-15 2:47
professionalD V L12-Sep-15 2:47 
Questionchat under main web page Pin
user37824754-Jun-15 8:36
user37824754-Jun-15 8:36 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA20-Jul-14 4:36
professionalȘtefan-Mihai MOGA20-Jul-14 4:36 
GeneralMy vote of 5 Pin
sibeesh19-Jul-14 3:04
professionalsibeesh19-Jul-14 3:04 
GeneralMy vote of 5 Pin
csharpbd12-Jul-14 10:53
professionalcsharpbd12-Jul-14 10:53 
GeneralMy vote of 5 Pin
CatchExAs12-Jul-14 6:15
professionalCatchExAs12-Jul-14 6:15 
QuestionOne question Pin
Tridip Bhattacharjee30-Jun-14 7:24
professionalTridip Bhattacharjee30-Jun-14 7:24 
AnswerRe: One question Pin
Kel_1-Jul-14 0:52
Kel_1-Jul-14 0:52 
GeneralRe: One question Pin
Tridip Bhattacharjee1-Jul-14 21:01
professionalTridip Bhattacharjee1-Jul-14 21:01 
GeneralRe: One question Pin
Kel_1-Jul-14 22:24
Kel_1-Jul-14 22:24 
GeneralRe: One question Pin
Tridip Bhattacharjee2-Jul-14 20:52
professionalTridip Bhattacharjee2-Jul-14 20:52 
GeneralMy vote of 5 Pin
Renju Vinod29-Jun-14 20:47
professionalRenju Vinod29-Jun-14 20:47 
Questionchat with angular Pin
b.hashmi29-Jun-14 2:49
b.hashmi29-Jun-14 2:49 
AnswerRe: chat with angular Pin
Kel_1-Jul-14 0:55
Kel_1-Jul-14 0:55 
QuestionWhat is wrong? Pin
Laurentiu LAZAR26-Jun-14 2:16
Laurentiu LAZAR26-Jun-14 2:16 
AnswerRe: What is wrong? Pin
Kel_28-Jun-14 1:15
Kel_28-Jun-14 1:15 
GeneralRe: What is wrong? Pin
Laurentiu LAZAR29-Jun-14 20:58
Laurentiu LAZAR29-Jun-14 20:58 
GeneralRe: What is wrong? Pin
Laurentiu LAZAR29-Jun-14 22:03
Laurentiu LAZAR29-Jun-14 22:03 
GeneralRe: What is wrong? Pin
Kel_1-Jul-14 0:45
Kel_1-Jul-14 0:45 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun24-Jun-14 21:14
Humayun Kabir Mamun24-Jun-14 21:14 
GeneralMy vote of 5 Pin
Volynsky Alex21-Jun-14 0:25
professionalVolynsky Alex21-Jun-14 0:25 
QuestionMy Vote of 5 Pin
newton.saber20-Jun-14 8:29
newton.saber20-Jun-14 8:29 
GeneralMy vote of 5 Pin
Member 1021161820-Jun-14 5:11
Member 1021161820-Jun-14 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.