Click here to Skip to main content
Click here to Skip to main content

HTML5 Chat with AngularJs, Spike Engine and Twitter Bootstrap

, 12 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

function NgChatCtrl($scope) {
    // Our server to connect to
    var server = new 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.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.onConnect(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.

<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

  • 20-06-14: Initial Version
  • 12-07-14: Deployment Explanation

License

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

Share

About the Author

Kel_
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants is the guy behind www.spike-engine.com project, a real-time, client-server networking layer (SOA, RPC) for .NET developers. Also the founder of Misakai Ltd..
 
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.

 
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalMihai MOGA20-Jul-14 5:36 
GeneralMy vote of 5 Pinprofessionalsibeesh19-Jul-14 4:04 
GeneralMy vote of 5 Pinmembercsharpbd12-Jul-14 11:53 
GeneralMy vote of 5 PinprofessionalCatchExAs12-Jul-14 7:15 
QuestionOne question PinmemberTridip Bhattacharjee30-Jun-14 8:24 
AnswerRe: One question PinprofessionalKel_1-Jul-14 1:52 
GeneralRe: One question PinmemberTridip Bhattacharjee1-Jul-14 22:01 
GeneralRe: One question PinprofessionalKel_1-Jul-14 23:24 
GeneralRe: One question PinmemberTridip Bhattacharjee2-Jul-14 21:52 
GeneralMy vote of 5 PinprofessionalRenju Vinod29-Jun-14 21:47 
Questionchat with angular Pinmemberb.hashmi29-Jun-14 3:49 
AnswerRe: chat with angular PinprofessionalKel_1-Jul-14 1:55 
QuestionWhat is wrong? PinmemberLaurentiu LAZAR26-Jun-14 3:16 
AnswerRe: What is wrong? PinprofessionalKel_28-Jun-14 2:15 
GeneralRe: What is wrong? PinmemberLaurentiu LAZAR29-Jun-14 21:58 
GeneralRe: What is wrong? PinmemberLaurentiu LAZAR29-Jun-14 23:03 
GeneralRe: What is wrong? PinprofessionalKel_1-Jul-14 1:45 
GeneralMy vote of 5 PinmemberHumayun Kabir Mamun24-Jun-14 22:14 
GeneralMy vote of 5 PinprofessionalVolynsky Alex21-Jun-14 1:25 
QuestionMy Vote of 5 Pinmembernewton.saber20-Jun-14 9:29 
GeneralMy vote of 5 PinmemberMember 1021161820-Jun-14 6:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Jul 2014
Article Copyright 2014 by Kel_
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid