The Modelit Embedded Webserver Toolbox for Matlab makes it possible to use Matlab as an HTTP server. But it also contains methods to send HTTP requests, these methods are similar to the Matlab webread function. But the methods in the toolbox offer some extras.

One of these extras is to monitor the progress of a running HTTP request. In case a request takes a long time the user can be informed about the progress which will improve the overall user experience.

For this example we will use the third party httpbin website that supplies large downloads for testing purposes. To replicate the example, copy paste the code at the end of this article in your own m-file and run it:

>> monitorprogress 
9% completed 
25% completed 
35% completed 
51% completed 
67% completed 
79% completed 
95% completed 
100% completed 
request -> 200 (102400 bytes received) 
>>

The example consists of the following steps:

  1. Create a modelit.web.client.HttpRequest to send an HTTP GET request to https://httpbin.org/bytes/102400. The response of this request is an array of 102400 random bytes. Note that progress monitoring is not restricted to only an HTTP GET method.
  2. Use the setCompletedCallback to define what to do when the response is received.
    This callback expects one argument (modelit.web.client.HttpResponse) with the server's response.
    Note that this step can be omitted. In that case the response will be returned by the send method and the Matlab execution thread will be blocked until the request has completed.
  3. Use the setProgressUpdatedCallback method to receive notifications about the progress.
    The callback will receive two arguments: The nl.modelit.web.client.HttpRequest object with which the request was made
    and a nl.modelit.web.client.ProgressEvent, This is a Java object with only one useful method, getProgress. 
  4. Use the getProgress method on the nl.modelit.web.client.ProgressEvent to get the progress. The value will be between 0 and 100%
    You can use this value for example in a Matlab waitbar to show the progress. 

Note that  this approach is an example of asynchronous computing. The Matlab thread is not blocked while the download is in progress. If this is not what you want, a typical work around would be to create a modal waitbar that remains posted while the download is in progress. 

function monitorprogress
% monitorprogress - Example of progress monitoring of an HTTP request.
%
% CALL:
%   monitorprogress
%
% INPUT:
%  No input required
%
% OUTPUT:
%  No output

% Define the properties of the HTTP request.
% Use httpbin.org for testing.
% If an HTTP GET request is sent to: The https://httpbin.org/bytes/102400
% 102400 random bytes are returned from the httpbin server.
METHOD = 'GET';
URL = 'https://httpbin.org/bytes/102400';

% Initialize the HTTP request
request = modelit.web.client.HttpRequest(METHOD, URL);
% Set the callback to be executed when the request has finished
request.setCompletedCallback(@completedCallback);
% Set the callback to process the
request.setProgressUpdatedCallback(@progressUpdatedCallback);
request.send;

%__________________________________________________________________________
function completedCallback(obj)
% completedCallback - Callback triggered when the request has completed.
%
% CALL:
%   completedCallback(obj)
%
% INPUT:
%   obj: <modelit.web.client.HttpResponse>
%     The response from the server
%
% OUTPUT:
%  No output

fprintf('request -> %d (%d bytes received)\n', obj.getResponseCode(), length(obj.data));

%__________________________________________________________________________
function progressUpdatedCallback(obj, event)
% progressUpdatedCallback -
%
% CALL:
%   progressUpdatedCallback(obj, event)
%
% INPUT:
%  obj: <nl.modelit.web.client.HttpRequest>
%    Java object with the HTTP request
%  event: <nl.modelit.web.client.ProgressEvent>
%    Contains information about the progress
%
% OUTPUT:
%  No output

% Get the progress in procent from the
progress = event.getProgress();

fprintf('%d%% completed\n', round(progress));