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.
- It uses HTML5 Data URI for rendering images streamed from the server as byte arrays and identicons for avatars.
- It uses CSS3 for styling and JQuery for animations.
- It uses custom fonts and Google's AngularJS for client-side.
- It is cross-platform and with a minimized packet payload and message compression.
- The application server is a self-hosted executable and the client is just a plain html file.
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) {
var server = new spike.ServerChannel('http://127.0.0.1:8002');
var side = 'left';
$scope.messages = [];
$scope.sendMessage = function () {
server.sendNgChatMessage($scope.messageText);
$scope.messageText = "";
};
server.on('ngChatMessagesInform', function (p) {
$scope.messages.push({
avatar: "data:image/png;base64," + p.avatar.toBase64(),
text: p.message,
side: side
});
$scope.$apply();
$("#viewport-content").animate({
bottom: $("#viewport-content").height() - $("#viewport").height()
}, 250);
side = side == 'left' ? 'right' : 'left';
});
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.
<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