Simple Messaging System with Redis and Node.js

Hey all, been a while since I posted anything constructive, I’ve been so busy wasting my time creating games for Facebook Instant Games Messenger (I will do a proper write up with my analysis and final findings / thoughts on this very soon). Not all has been lost working on Instant Games however. Two of my games required an instant messaging system that enables me to send real-time messages between players which ironically the Instant Games SDK doesn’t cater for. So I created one using Node.js and Redis (these two bad boy pieces of tech together are like sweet music). You can grab the code from Github here.

Note that you will need to install this to your own server, I like to run most of my node modules using PM2. So to get the messaging system up and running just run pm2 msys. The server looks to the /msys endpoint, but you can change this in msys.js if you need it to go elsewhere. No, I don’t answer questions on how to set up servers and mess with Apache config files because I hate all that junk, it gets in the way of my actual coding which I do enjoy :) . If you cannot do this stuff yourself then you probably should be paying someone else do this for you.

Oh word of warning, any messages sent will time out after 7 days (this is to keep Redis memory usage down), but you can extend this to whatever time limit you like. Messages are queued, when you collect the pending messages it collects them all and deletes them from the database.

Ok, how to use client side? Here is a simple class (erm I mean collection of functions) with an awesome original name that I ripped out of one of my games for you guys to use:

var Backend = {};

Backend.SendMessage = function(data, done_callback)
{
    var url = "https://yourdomain.com/msys?c=s&t=<your token>";
    url += "&g=1";
    url += "&u=" + data.to_id;
    url += "&d=" + encodeURIComponent(JSON.stringify(data));
    b5.Utils.SendGetRequest(url, function(response) {
        if (done_callback !== undefined)
            done_callback(response);
    })
}

Backend.SendMessageMulti = function(recipients, data, done_callback)
{
    var users = "";
    var len = recipients.length;
    for (var t = 0; t < len; t++)
    {
        users += recipients[t];
        if (t < (len - 1))
            users += "_";
    }
    var url = "https://yourdomain.com/msys?c=s&t=<your token>";
    url += "&g=1";
    url += "&m=" + users;
    url += "&d=" + encodeURIComponent(JSON.stringify(data));
    b5.Utils.SendGetRequest(url, function(response) {
        if (done_callback !== undefined)
            done_callback(response);
    })
}

Backend.GetMessages = function(done_callback)
{
    var url = "https://yourdomain.com/msys?c=g&t=<your token>";
    url += "&g=1";
    url += "&u=" + Social.GetPlayerID();
    b5.Utils.SendGetRequest(url, function(response) {
        if (response.status == 200)
        {
            var data = decodeURIComponent(response.responseText);
            var obj = JSON.parse("[" + data + "]");

            if (done_callback !== undefined)
                done_callback(obj);
        }
        else
        {
            if (done_callback !== undefined)
                done_callback();
        }
    })
}

There are a few functions in here that you will need to implement yourself:

  • Social.GetPlayerID() – Replaced with your players user ID, if you are using Facebook Instants SDK then use FBInstant.player.getID()
  • b5.Utils.SendGetRequest() – Performs a GET request, e.g:
b5.Utils.SendGetRequest = function(url, callback)
{
    var req = new XMLHttpRequest();
    req.onreadystatechange = function()
    {
        if (callback != undefined && req.readyState == 4)
            callback(req);
    }
    req.open("GET", url, true);
    req.send();
}

Anyway, that’s it, hope you find more useful than I did. Happy coding :) .

Leave a Reply