Rajeev
Rajeev

Reputation: 46909

Bandwidth utility using javascript

IS there any utility to measure bandwidth from my server to the client or any standard APIS.Need this for web application

Upvotes: 8

Views: 18607

Answers (3)

doublemax
doublemax

Reputation: 473

    // Network connection - https://github.com/daniellmb/downlinkmax
var connectionSpeed = function() 
{
    // Deal with vendor prefixes
    var defaultSpeed = false,
        navigator = window.navigator,
        connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection || null;
    if( ! connection )
        return defaultSpeed;

    // assume W3C Editor's Draft 09 October 2014
    if( 'downlinkMax' in connection )
    {
        var downlinkMax = connection.downlinkMax;
        if( ! downlinkMax )
            return defaultSpeed;
        if( ! isFinite(downlinkMax) )
            return defaultSpeed;
        return downlinkMax;
    }
    // assume W3C Working Draft 29 November 2012
    if( 'bandwidth' in connection )
    {
        var bandwidth = connection.bandwidth;
        if( ! bandwidth )
            return defaultSpeed;
        if( isNaN(speed) )
            return defaultSpeed;
        // standardize connection.bandwidth value by converting megabytes per second (MB/s) to megabits per second (Mbit/s)
        return bandwidth * 8;
    }
    // assume W3C Working Draft 07 June 2011
    switch( connection.type )
    {
        // convert connection.type value to approximate downlink values
        // speed estimate is based on the median downlink value for common devices in megabits per second (Mbit/s)
        case 'none':
            return 0;
        case '2g':
            return 0.134;
        case 'bluetooth':
        case 'cellular':
            return 2;
        case '3g':
            return 8.95;
        case '4g':
            return 100;
        case 'ethernet':
            return 550;
        case 'wifi':
            return 600;            
    }
    return defaultSpeed;
};

Upvotes: 1

Adrian Ber
Adrian Ber

Reputation: 21360

I needed something similar, so I wrote https://github.com/beradrian/jsbandwidth, a rewrite of https://code.google.com/p/jsbandwidth/.

The idea is to make two calls through Ajax, one to download and the other to upload through POST.

It should work with both jQuery.ajax or Angular $http.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074148

Don't know of any utility or standard API, no, but you can do this by having several images of various sizes on your website, and then retrieving them bypassing cache and watching how long it takes for them to load. That information, along with the size of the image, gives you an indication of the speed between the two endpoints.

The reason for using multiple images is that you'll want to start small (say, 20k), but if the connection is fast that will give you a very inaccurate number; so based on how quickly that image loaded, you'll want to select another image of an appropriate size to try to get a better idea of the actual bandwidth (as opposed to the latency setting up the connection and such).

You can do this just with straight JavaScript, by adding img tags off-page with a unique query string to bypass caching; but as you've tagged your question "jQuery", you may find it more convenient to use the .ajax function (with its cache: false setting) instead.

The speed number you come up with is only an indication, since other things could be going on that mess up your timing (video streaming in another tab — or on another computer hooked to the same internet connection, even other things slowing up the execution of your JavaScript, like a JS-heavy animation on the page), but it should be good enough to give you an idea what you're working with.

Upvotes: 22

Related Questions