The Modelit Webserver Toolbox for Matlab makes it possible to use Matlab as an HTTP server. But it also contains methods to send HTTP requests. One of the advantages of using this toolbox is that Http requests can be sent asynchronously.
In this way the Matlab thread is not blocked and one can use fact that most servers can process multiple requests simultaneously. Modelit uses this for example in the MapViewer in which multiple map tiles are loaded simultaneously from an external server without blocking the Matlab application.
For this example we will use the httpbin website to download files with a delay. You will find the complete code at the end of this article. Just create a .m file in Matlab and copy paste this code and type the following command in the Matlab console to run the example:
>> testasync
request 1 sent
request 1 -> 200
request 2 sent
request 2 -> 200
request 3 sent
request 3 -> 200
request 4 sent
request 4 -> 200
request 1 sent
request 2 sent
request 3 sent
request 4 sent
request 3 -> 200
request 4 -> 200
request 2 -> 200
request 1 -> 200
>>
Note that for the second part of the example where the Http requests are sent asynchronously the responses are returned in a different and unpredictable order.
The example consists of the following steps:
- Create a number of modelit.web.client.HttpRequest to send an HTTP GET request to https://httpbin.org/delay/3. The httpbin server will process this request and return the response after a delay of 3 seconds.
- Send the created requests synchronously, this is equivalent to the Matlab webread function. The next request will be started when the previous request has completed. And the result will be collected in the send method's output argument.
- Send the created requests asynchronously, An HttpRequest is sent asynchronously when the completedCallback is set. This callback defines what to do when the response is received. This callback expects one argument (modelit.web.client.HttpResponse) with the server's response. Note that contrary to the synchronous request no output argument is available with the send method.
function testasync % testasync - Example of asynchronous HTTP requests. % % CALL: % testasync % % 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/delay/3 % a response is returned with a 3 second delay. METHOD = 'GET'; URL = 'https://httpbin.org/delay/3'; N = 4; % Use the synchronous modelit.web.client.HttpRequest, the requests are made in order % and similar to webread the Matlab execution thread is blocked until the % request has completed for i=1:N r = modelit.web.client.HttpRequest(METHOD, URL); fprintf('request %d sent\n', i); result = r.send; fprintf('request %d -> %d\n', i, result.getResponseCode()); end % Use the asynchronous modelit.web.client.HttpRequest. By using the % setCompletedCallback method, all requests are sent at once. The callbacks % are triggered upon arrival (in random order) of the server's responses. for i=1:N fprintf('request %d sent\n', i); r = modelit.web.client.HttpRequest(METHOD, URL); % Set the callback to be executed when the request has finished r.setCompletedCallback(@(result)completedCallback(i, result)); r.send; % the result is handled by the completedCallback function. end %__________________________________________________________________________ function completedCallback(i, result) % completedCallback - Callback triggered when the request has completed. % % CALL: % completedCallback(i, result) % % INPUT: % i: <int> % number of the request, for display purposes % result: <modelit.web.client.HttpResponse> % The response from the server % % OUTPUT: % No output fprintf('request %d -> %d\n', i, result.getResponseCode());