Category Archives: javascript

Development of a Swift Vapor App using WebSockets

I have been working with WebSockets since their release. While I like the idea of a browser based full duplex communication between the client and server one of my favorite interests with WebSockets has been getting multiple browser based apps to communicate with each other in realtime.

In this article I will give an example for a realtime application where users can collaborate on a shared html5 canvas using a jQuery plugin I wrote a few years back. I will be using Swift on the server side with the web framework Vapor.

For more information about the plugin that I developed you can review an article that I wrote for it here.

Requirements

You will of course need to have Swift 3.x and Vapor installed on your local machine and if you haven’t already done this there is a great step by step tutorial you can follow here.

Don’t stop here If you think Swift is only for Apple OS’s because it can actually be run on multiple OS’s and the above instructions will given you an example of setting it up to run on Ubuntu. I wrote this article demonstrating how to setup a Virtual Ubuntu machine for Vapor.

Finally the rest of this post assumes you already have a basic understanding of HTML, Javascript, Swift and Vapor. For those wanting to learn more about Vapor there are some great how to videos and articles to get started here.

Setting up Client

I don’t plan to cover the details of laying out the page but for a quick demonstration of what everything will look you can go here.

Talking with Server

To make communications between the clients simple I will be using JSON objects.

The following code will be used to store and manage the WebSocket communications that are sent and received:

function dooScribConnection(host) {
    var scrib = this;
    scrib.ws = new WebSocket('wss://' + host);
            
    scrib.ws.onopen = function() {
        var connRequest = JSON.stringify({'command': 'connect', 'username': id})
        scrib.ws.send(connRequest)
    };
            
    scrib.ws.onmessage = function(event) {
        var msg = JSON.parse(event.data);
                
        if (msg.command == "click") {
            prevPoint[msg.username] = msg;
        } else if (msg.command == "paint") {
            surface.drawLine(prevPoint[msg.username].X, prevPoint[msg.username].Y, msg.X, msg.Y, msg.color, msg.pen);
            prevPoint[msg.username] = msg;
        } else if (msg.command == "clear") {
            surface.clearSurface();
        }
    };
            
    scrib.send = function(event) {
        scrib.ws.send(event);
    };
};

The connection to the server is established as soon as the WebSocket object is created. The onOpen function is called once that connection is established. By making a call to send information to the server here it will allow the server to store instance information about each of its connections. To make sure that each user is uniquely identified the id var is established using the following:

var id = Math.round($.now()*Math.random());

For messaging to the server I added the send function which will send the JSON objects that are passed as a parameter to it.

Whenever a message is received the onmessage function is called and from there you can switch on the command type (added to the JSON message) and take the appropriate action. Currently the following commands (JSON objects) are of interest for handling inside the browser:

  • click – this is used to establish the starting point for when the user has started drawing.
  • paint – this is used to draw a line from the previous point to the current one.
  • clear – a command that allows users to clear the drawing canvas.

Setting up Canvas (DooScrib plugin)

The following code is for setting up the jquery plugin as soon as the page has been loaded and is ready:

$(document).ready(function(){
    var height = $('#raw() { #surface }').height();
    var width = $('#raw() { #surface }').width();

    dooscrib = new dooScribConnection(window.location.host + "/dooscrib");
    surface = new $('#raw() { #surface }').dooScribPlugin({
        width:width,
        height:400,
        cssClass:'pad',
        penSize:4,
                                                                  
        onMove:function(e) {
            var msg = JSON.stringify({'command': 'mousemove', 'username': id, 'X': e.X, 'Y': e.Y });
            dooscrib.send(msg);
        },
                                                                  
        onClick:function(e) {
            var msg = JSON.stringify({'command': 'click', 'username': id, 'X': e.X, 'Y': e.Y});
            dooscrib.send(msg);
        },
                                                                  
        onPaint:function(e) {
            var msg = JSON.stringify({'command': 'paint', 'username': id, 'X': e.X, 'Y': e.Y,'pen': surface.penSize(),'color':surface.lineColor()});
            dooscrib.send(msg);
        },
                                                                  
        onRelease:function(e) {
            var msg = JSON.stringify({'command': 'release', 'username': id, 'X': e.X, 'Y': e.Y});
            dooscrib.send(msg);
        }
    });
});

The drawing for the current surface is handled by the plugin which then messages everything that it is being done. With each message there is JSON message created which is then sent to the server which will redirect to all the other users.

Setting up Server

For the server implementation I created the following Controller object which handles all of the WebSockets communications as well as routes for the different web pages:

final class ScribController {
    var dooscribs : [String: WebSocket]
    var droplet : Droplet

    init(drop: Droplet) {
        dooscribs = [:]
        droplet = drop

        droplet.get("", handler: scribRequest)
        droplet.get("about", handler: aboutRequest)
        droplet.socket("dooscrib", handler: socketHandler )
    }
    
    func aboutRequest(request: Request) throws -> ResponseRepresentable {
        return try droplet.view.make("about")
    }

    func scribRequest(request: Request) throws -> ResponseRepresentable {
        return try droplet.view.make("socket")
    }
    
    func socketHandler(request: Request, socket: WebSocket) throws {
        var scribUser: String? = nil
        
        // create an active ping to keep connection open
        try background {
            while socket.state == .open {
                try? socket.ping()
                drop.console.wait(seconds: 5)
            }
        }
        
        socket.onText = { socket, message in
            let json = try JSON(bytes: Array(message.utf8))
            
            guard let msgType = json.object?["command"]?.string, let user = json.object?["username"]?.string else {
                return
            }
            
            if msgType.equals(any: "connect") {
                scribUser = user
                self.dooscribs[user] = socket
                
                let response = try JSON(node: [
                    "command":"connected",
                    "username": user
                    ])
                
                // send a connect response to everyone including self
                for (_, connection) in self.dooscribs {
                    try connection.send(response)
                }
            } else if (msgType.equals(any: "clear")) {
                for (_, connection) in self.dooscribs {
                    try connection.send(json)
                }
            } else {
                // send message to everyone (minus self)
                for (scrib, connection) in self.dooscribs {
                    if (!scrib.equals(any: user)) {
                        try connection.send(json)
                    }
                }
            }
        }
        
        socket.onClose = { ws, _, _, _ in
            guard let user = scribUser else {
                return
            }
            
            let disconn = try JSON(node: [
                "command": "disconnect",
                "username": user
                ])
            
            // tell everyone (minus self) about disconnect
            for (remote, connection) in self.dooscribs {
                if (!remote.equals(any: user)) {
                    try connection.send(disconn)
                }
            }
            
            self.dooscribs.removeValue(forKey: user)
        }
    }
}

I won’t be covering the get route handlers that are established in the controller with any great detail. From the code you however you can see that they are sending back the pages the user requested.

Sending JSON Messages

Much to my surprise the WebSocket class does not have a function for sending JSON packets so I created the following extension to handle that:

extension WebSocket {
    func send(_ json: JSON) throws {
        let data = try json.makeBytes()
        
        try send(data.string)
    }
}

Handling Messages

Messages are handled via the onText EventHandler which is called with a WebSocket and String passed in via closure. The string data is parsed into a JSON object so that command and user id that should be sent with each message.

For the connect message the user id and accompanying WebSocket are saved in the dooscribs dictionary and then a connected message is created and, much like the clear message, it gets to sent to all of the connections.

The other messages that are received get forwarded to all of the current connections with an exception for the one where the message.

Handling Connections and Users

Each connection will be stored in the dooscribs dictionary which is a pairing of the unique id that was generated by the browser and the WebSocket for their connection.

Whenever a connection is closed (user leaves page, closes browser) the onClose handler is called informing of the event. For cleanup purposes send a disconnect message to all of the clients that are stored in the dooScribs dictionary, minus the connection that just closed, as well as remove that user from the dictionary.

Issues – Random Disconnection

Something I noticed in my development was that clients were disconnecting for what seemed at the time as unknown reasons. After some research I found that “quiet” connections will automatically get disconnected. To handle this I created the background handler so that it would ping the socket every 5 seconds to keep channels open.

Conclusion

Hopefully this gives you some ideas for processing realtime data in your applications. In the future I plan to expand the dooScrib application so that it can handle different rooms as well text based messaging. The code for the dooScrib plugin as well a Node.js Implementation and this Vapor implementation can be founder here.

For now enjoy and as always Happy Coding!!!

tick, tock… Time waits for No One

Ok so what I am about to discuss might be old news for most but for me it’s new. After discovering this I have to admit that I am a bit disappointed in myself for not already knowing this.

When implementing asynchronous methods in Node I had always done the following:

function asynchProcessFunc = function(callback) {
    setTimeout(function(callback){
        // process the request now
        callback(returnData);
    }, 0);

    // return back 
};

It was recommended by someone that I change this out for the process.nextTick(cb) method. Being a firmware developer in previous lifetimes the pure mention of tick had me intrigued.

Was it actually processing CPU ticks?

Turns out the answer was No but still what I did find was something far better than a zero based timeout. I had always known that setTimeout(cb, timer) with a zero based timer was never going to be accurate but I had decided it was the best I could ever get.

To get an idea of a difference between the two I wrote the following to measure setTimeout:

setTimeout(function(){
    var startTime = process.hrtime();

    setTimeout(function() {
        var timeDiff = process.hrtime(startTime);
        console.log('setTimeout time diff - ', timeDiff[0]*1e9 + timeDiff[1], '(nanoSecs)');
    }, 0);
}, 1000);

I rounded the average rate to 1600000.

I then changed the program to use process.nextTick() like the following:

setTimeout(function(){
    var startTime = process.hrtime();

    process.nextTick(function() {
        var timeDiff = process.hrtime(startTime);
        console.log('nextTick time diff - ', timeDiff[0]*1e9 + timeDiff[1], '(nanoSecs)');
    });	
}, 1000);

I rounded the average rate to 400000, which gives me a difference of about 4x performance increase. Depending on your product this I am sure will change but even the improvement comes in at 2x that is still a major performance increase.

Happy coding everyone.

Wednesday fun (maybe not)

All work and no play makes Jack Dennis a dull null boy.


rock picture

So one of the wonderful things about living here is that regardless the season we always have something great to do outside. During this time of year we go on a hike each Wednesday with a group of friends. Usually a great time to unwind and not have to think about anything work.

The trick or struggle always for me is to not think about anything work related and to just relax and enjoy the surroundings. I always enjoy spending the time outdoors whether on my bike, camping with family or on a hike like tonight. However without fail I am always working out a problem in my head at some point.

So I am proud to say that I actually made it through the hike without thinking about any problems I am currently working on. Breakthrough!!!

Tonight I created a new problem and worked on it during the hike. How is that for progress?

Recently I saw a demo on AngularJS and I was thinking on my hike how hard would it be to redo a jQuery plugin I did for displaying a twitter? The following is what I came up with:

Twitter Feed

  • {{tweet.text}}

For now I was thinking of something really simple. Once I got it pulling data from twitter and displaying something I knew that the rest would come easy.

So before I get deep into the code of AngularJs let me give a sample of the jQuery plugin I had done awhile back.

(function($){
    $.fn.minTweet = function(options) {
        var defaults = {
            tweets: 5,
            before: "
  • ", after: "
  • ", click: function() {}, onComplete: function() {} }; var tweetDeck = this; var options = $.extend(defaults, options); var tweetStack = new Array(); var init = function() { tweetDeck.each(function() { var obj = $(this); $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name='+options.username+'&count='+options.tweets, function(data) { $.each(data, function(i, tweet) { tweetStack[i] = tweet; if(tweet.text !== undefined) { $(options.before+twitterTime(tweet.created_at)+options.after).appendTo($(obj)).attr('title', tweet.text).attr('id', i).bind('click.minTweet', twitterClick); } }); options.onComplete(); } ); }); }; var twitterClick = function() { if(tweetStack[$(this).attr('id')]) { options.click(tweetStack[$(this).attr('id')]); } }; var twitterTime = function(dts) { var now = new Date(); var then = new Date(dts); var diff = now - then; var second = 1000, minute = second * 60, hour = minute * 60, day = hour * 24, week = day * 7; if (isNaN(diff) || diff < 0) { return "Unkown"; // return blank string if unknown } else if (diff < minute) { return Math.floor(diff / second) + " seconds ago"; } else if (diff < minute * 2) { return "1 minute ago"; } else if (diff < hour) { return Math.floor(diff / minute) + " minutes ago"; } else if (diff < hour * 2) { return "1 hour ago"; } else if (diff < day) { return Math.floor(diff / hour) + " hours ago"; } else if (diff > day && diff < day * 2) { return "yesterday"; } else if (diff < week) { return Math.floor(diff / day) + " days ago"; } else if (diff > week && (diff < (week + day))) { return "a week ago"; } else { return Math.floor(diff / day) + " days ago"; } }; init(); }; })(jQuery);

    Probably not one of my greatest achievements but at the time of it's creation it was simple and served to solve a problem that I was working on at the time. Granted a majority of the code is that twitterTime() function it always bothered me that I should have been able to make it simpler.

    So would AngularJs save me or would my idea burn? Here is the javascript that I came up with.

    function twitterFeed($scope, $http) {
        var url = 'https://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name=dniswhite&count=1';
        $http.jsonp(url).success(function(data,status){
            $scope.tweets = data;
        }).error(function(data) {
            $scope.tweets = "Request failed";
        });          
    }
    

    For now I hardcoded the name of the twitter feed being retrieved to mine. As I went to test it I could see that the error() function was being called. What could be wrong with my perfectly small piece of code?

    Fire off my old jQuery plugin and sure enough it's no longer working. Did I ever deploy this code and if so what site did I deploy it on? Somewhere out there is a site with a broken twitter feed.

    Take out the url from the code and lets give this a test.

    {"errors": [{"message": "The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}
    

    So it appears that for now I am temporarily stuck and I will need to resolve the new API (this requires OAuth). I hate to end blogs without resolution so instead let me say "Stay Tuned"

    Maybe it wasn't such a breakthrough to not think about problems during tonights hike.