Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML5

HTML5 Chat with AngularJs, Spike Engine and Twitter Bootstrap

4.91/5 (47 votes)
23 Jun 2015CPOL3 min read 136.4K   6.6K  
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)