Click here to Skip to main content
15,126,498 members
Articles / Web Development / HTML
Posted 15 Aug 2013


89 bookmarked

HTML5 Real-Time Chat with Websockets, jQuery, and Spike-Engine

Rate me:
Please Sign up or sign in to vote.
4.90/5 (40 votes)
23 Jun 2015CPOL3 min read
MyChat: a simple real-time client-server chat using fancy HTML5 features and identicons.


Everything in the web becomes increasingly real-time and HTML5 finally offers some facilities to build efficient, simple and robust real-time applications on the web. This article serves as a demonstration of such facilities in building a fun and simple client-server chat: 

  1. It uses websockets internally, but abstracted by Spike-Engine, will fallback to flash sockets for older browsers.
  2. It uses HTML5 Data URI for rendering images streamed from the server as byte arrays.
  3. It uses custom fonts and jQuery for animated rendering.
  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.

Image 1

[view a live demo]  

 Client-Server Protocol    

First of all, we need to define a communication protocol between. This represents the messages which are exchanged between the client and the server. Spike-Engine simplifies the networking implementation and allows expressing this protocol declaratively via an XML file. This part is quite straightforward, check the user guide on their website for more info. 

<?xml version="1.0" encoding="UTF-8"?>
<Protocol Name="MyChatProtocol" xmlns="" 

    <!-- Simple join chat operation that puts the client to the chat room -->
    <Operation Name="JoinMyChat"
               SuppressSecurity="true" />

    <!-- Simple send message operation that broadcast the message to the chat room -->
    <Operation Name="SendMyChatMessage"
        <Member Name="Message" Type="String" />

    <!-- A push operation (server to client) that sends the messages to the clients -->
    <Operation Name="MyChatMessages"
        <Member Name="Avatar" Type="ListOfByte" />
        <Member Name="Message" Type="String" />

Essentially, we've declared 3 message types:

  1. JoinMyChat: initiated by the client who wants to join the chat room. Server will add the client to the room (a list of clients) and generate an identicon to be used as an avatar for that client.
  2. SendMyChatMessage: initiated by the client and contains a string message to send to everyone in the chat room.
  3. MyChatMessages: initiated by the server, sends the avatar and the message to everyone in the room. This is an event of type push, it is pushed to the clients. More info:

Because we are using Spike-Engine to abstract our websockets/flashsockets communication, the network layer is automatically generated, similarly to web services. I won't go into the code generation part here, as a step-by-step explanation can be found here: and is out of the scope of this article.

Identicons and HTML5 Data URI 

In our chat application, I wanted to use avatars, but I didn't want the users to log-in or chose one, it had to be automatically generated. But not randomly, same IP address should have the same avatar, identifying the person even if he disconnects/reconnects or uses a different computer to chat. Identicons are the right answer for that job.  An Identicon is a visual representation of a hash value, usually of an IP address, that serves to identify a user of a computer system as a form of avatar while protecting the users' privacy. 

Image 2

How to generate them? Jeff Atwood, from stackoverflow wrote a port of such library: And I used their open-sourced library to do this:, with some small alterations. The rendering is straightforward. 

public byte[] Render(IClient client)
    using (var stream = new MemoryStream())
        // Get the code from the ip address
        var code = IdenticonUtil.Code(client.Channel.Address.ToString());

        // Generate the identicon
        var image = this.Render(code, 50);

        // Save as png
        image.Save(stream, ImageFormat.Png);
        return stream.ToArray();

The interesting part is that on each message, we want to send the avatar of the user, represented as a byte[]. But how do we actually render it once the JavaScript client received the array? The answer is the new HTML5 feature called Data URI. 

"The data URI scheme is a URI scheme (Uniform Resource Identifier scheme) that provides a way to include data in-line in web pages as if they were external resources. This technique allows normally separate elements such as images and style sheets to be fetched in a single HTTP request rather than multiple HTTP requests, which can be more efficient." - From Wikipedia's entry on Data URI

The following code snipped shows how we render the byte array received by javascript and convert it to a data uri. The final step is just assigning that URI to an <img src=''> element.

// Get the bytes of the image and convert it to a BASE64 encoded string and then
// we use data URI to add dynamically the image data
var avatarUri = "data:image/png;base64," + avatar.toBase64();  

Appendix A: Server-Side Implementation 

For the sake of completeness of this article, here is the full server-side implementation of the chat, exluding the identicon file. 

public static class MyChatImpl
    /// <summary>
    /// A public static function, when decorated with InvokeAt attribute will be
    /// invoked automatically by Spike Engine. This particular one will be invoked
    /// when the server is initializing (only once).
    /// </summary>
    public static void Initialize()
        // First we need to hook the events while the server is initializing, 
        // giving us the ability to listen on those events.
        MyChatProtocol.JoinMyChat += new RequestHandler(OnJoinChat);
        MyChatProtocol.SendMyChatMessage += new RequestHandler<SendMyChatMessageRequest>(OnSendMessage);

        // Since we will add the clients in the chat, we need to remove them from
        // the chat room once they are disconnected. 
        Service.ClientDisconnect += new ClientDisconnectEventHandler(OnClientDisconnect);

    /// <summary>
    /// A list of all clients currently in the chat. This is a concurrent list that 
    /// helps us to avoid most of the concurrency issues as many clients could be 
    /// added/removed to the list simultaneously.
    /// </summary>
    public static ConcurrentList<IClient> PeopleInChat = new ConcurrentList<IClient>();

    /// <summary>
    /// Static method that is ivoked when a client sends a message to the server.
    /// </summary>
    static void OnSendMessage(IClient client, SendMyChatMessageRequest packet)
        // Validate the message
        var message = packet.Message;
        if (message == null || message == String.Empty || message.Length > 120)

        // We loop through all people in the chat and we broadcast them
        // the incoming message.
        foreach (var person in PeopleInChat)
            // Send the message now
                (byte[])client["Avatar"], // The avatar of the client who sends the message
                message            // The message to be sent

    /// <summary>
    /// Static method that is ivoked when a client decides to join the chat.
    /// </summary>
    static void OnJoinChat(IClient client)
        // We add the person to the chat room
        if (!PeopleInChat.Contains(client))
            // Generate the avatar for the client
            client["Avatar"] = IdenticonRenderer.Create(client);

            // Add the person in the room

            // Say something nice
            SayTo(client, "Hi, I'm the author of this sample, hope you like it! 
              Just type a message below and see how it works on CodeProject.");

    /// <summary>
    /// Static method that is invoked when a client is disconnected.
    /// </summary>
    static void OnClientDisconnect(ClientDisconnectEventArgs e)
        // Remove the client from the list if he's 
        // disconnected from the chat
        if (PeopleInChat.Contains(e.Client))

    /// <summary>
    /// Author says something to a particular client in the chat room.
    /// </summary>
    static void SayTo(IClient client, string message)
        client.SendMyChatMessagesInform(Resources.Author, message);


Appendix B: Client-Side Implementation   

The client-side implementation is a bit more tricky, as there's some JQuery for visualisation and css files. Everything is included in the source package attached to this codeproject article. 

    var bubbles = 1;
    var maxBubbles = 8;
    var server;

    function sendMessage() {

    function addBubble(avatar, text) {

        // Get the bytes of the image and convert it to a BASE64 encoded string and then
        // we use data URI to add dynamically the image data
        var avatarUri = "data:image/png;base64," + avatar.toBase64();

        var bubble = $('<div class="bubble-container"><span ' + 
          'class="bubble"><img class="bubble-avatar" src="' + 
          avatarUri + '" /><div class="bubble-text"><p>' + text + 
          '</p></div><span class="bubble-quote" /></span></div>');


        if (bubbles >= maxBubbles) {
            var first = $(".bubble-container:first")

        $('.bubble-container').show(250, function showNext() {
            if (!($(this).is(":visible"))) {

                .show(250, showNext);


    // On page loaded and ready
    $(window).load(function () {

        // First we need to create a server channel on the given URI, in a form http://IP:PORT
        // For your local test you might try (or a different IP address/port)
        server = new spike.ServerChannel('');

        // When the browser is connected to the server, we show that we are connected to the user
        // and provide a transport name (websockets, flashsockets etc.).
        server.on('connect', function () {
            // Once connected, we need to join the chat


        // Here we hook the room messages inform event so we know when
        // the server sends us the messages. We need to show them properly in the text area.
        server.on('myChatMessagesInform', function (p) {
            addBubble(p.avatar, p.message);




  • 23/06/2015 - Source code & article updated to Spike v3
  • 14/09/2013 - Updated source code references & links. 
  • 16/08/2013 - Live demo link. 
  • 15/08/2013 - Initial article.


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


About the Author

Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants, Ph.D. is the architect behind 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

Questioninstalling? Pin
Member 1494227119-Sep-20 1:13
MemberMember 1494227119-Sep-20 1:13 
QuestionIntranet implementation Pin
abascalufe24-Jul-15 9:38
Memberabascalufe24-Jul-15 9:38 
AnswerRe: Intranet implementation Pin
Kel_11-Aug-15 7:21
MemberKel_11-Aug-15 7:21 
GeneralRe: Intranet implementation Pin
abascalufe11-Aug-15 12:10
Memberabascalufe11-Aug-15 12:10 
QuestionDoes this chat support file transfer? Pin
Tridip Bhattacharjee28-Dec-14 7:09
professionalTridip Bhattacharjee28-Dec-14 7:09 
QuestionClient only returning SingleChannelClient? Pin
Logan Apple26-Oct-14 6:24
professionalLogan Apple26-Oct-14 6:24 
AnswerRe: Client only returning SingleChannelClient? Pin
Kel_26-Oct-14 12:01
MemberKel_26-Oct-14 12:01 
GeneralRe: Client only returning SingleChannelClient? Pin
Logan Apple26-Oct-14 14:43
professionalLogan Apple26-Oct-14 14:43 
GeneralRe: Client only returning SingleChannelClient? Pin
Logan Apple7-Nov-14 12:16
professionalLogan Apple7-Nov-14 12:16 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun14-Oct-14 18:08
MemberHumayun Kabir Mamun14-Oct-14 18:08 
Questionsimultaneous message bug Pin
Member 105722384-Feb-14 17:33
MemberMember 105722384-Feb-14 17:33 
QuestionSo Good Pin
lizhujiang26-Jan-14 5:26
Memberlizhujiang26-Jan-14 5:26 
QuestionRunning on Mono Ubuntu 13.10 Pin
Member 104532348-Dec-13 13:27
MemberMember 104532348-Dec-13 13:27 
SuggestionRe: Running on Mono Ubuntu 13.10 Pin
Member 104532348-Dec-13 13:46
MemberMember 104532348-Dec-13 13:46 
AnswerRe: Running on Mono Ubuntu 13.10 Pin
Kel_9-Dec-13 8:59
MemberKel_9-Dec-13 8:59 
GeneralMy vote of 5 Pin
Phat (Phillip) H. VU17-Sep-13 21:10
MemberPhat (Phillip) H. VU17-Sep-13 21:10 
SuggestionI can make it only with jQuery! Pin
Tuyen Nguyen Quang 16-Sep-13 19:32
MemberTuyen Nguyen Quang 16-Sep-13 19:32 
GeneralRe: ?! Pin
Kel_16-Sep-13 11:00
MemberKel_16-Sep-13 11:00 
GeneralMy vote of 5 Pin
Carsten V2.014-Sep-13 21:41
MemberCarsten V2.014-Sep-13 21:41 
GeneralMy vote of 5 Pin
Uday P.Singh10-Sep-13 1:46
MemberUday P.Singh10-Sep-13 1:46 
GeneralMy vote of 5 Pin
DaoNhan9-Sep-13 17:47
MemberDaoNhan9-Sep-13 17:47 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun5-Sep-13 22:04
MemberHumayun Kabir Mamun5-Sep-13 22:04 
GeneralMy vote of 5 Pin
Prasad Khandekar16-Aug-13 3:15
professionalPrasad Khandekar16-Aug-13 3:15 
GeneralMy vote of 5 Pin
Nitij15-Aug-13 7:04
professionalNitij15-Aug-13 7:04 
QuestionIdea Pin
Member 1021161815-Aug-13 5:08
MemberMember 1021161815-Aug-13 5:08 

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.