Quantcast
Viewing latest article 2
Browse Latest Browse All 15

Mobile Network Programming

Developing widgets to run on mobile phones requires more careful attention to network latency, since unlike the desktop connected environment, the mobile data connection speed can vary and fail. Also, widgets are different than mobile web sites since they will launch even when the phone is completely offline.

Three general rules when performing network calls from a mobile device are the following:

  1. Always let the user know that there is a network call being made, perhaps with an animated gif with explanatory text. See: http://ajaxload.info
  2. Never block and freeze the user interface with a synchronous call, all network calls should be performs asynchronously. Many native frameworks will enforce this rule explicitly.
  3. Understand your timeout mechanisms and have a plan B for what to do in the case of a timeout.

There are a variety of ways to make asynchronous calls to get data off the network, from using XMLHTTPRequest, hidden iframes, or dynamic script tags. A general design pattern for putting timeout handling around your network calls looks like this:

  1. Label each request with a request ID that the server will echo back in the response.
  2. put the request ID and it's associated callback function in a local "pending request" queue.
  3. register a setTimeout(); to check the status of the request
  4. if the network call returns before the timeout, remove the request ID from the queue and clear the timeout using clearTimeout();
  5. if the timeout occurs, cancel the request, remove the request ID from the queue, and call the callback function with the appropriate timeout error code.

The NextDB.net JavaScript API for data storage is a classic example of a connected service for widgets. As such, the API has a variety of mechanisms for handling time outs. For any request you can set the timeout on the Connection object:


	... 
	var conn = new nextdb.Connection('ACCOUNT','DATABASE_NAME');
	conn.setTimeout(15);
	var query = new net.nextdb.Query("QUERY1");
	conn.executeQuery(query,
		function(rows,error){
			if(error){   
				switch (error.code){
					case : 408 // timeout
					...        
					}     
				} else {
				... 

any subsequent request, e.g. queries, inserts, updates and deletes will timeout in the specified duration and the callback will be passed a timeout notification, at which point you can offer the user options such as trying again.

There are also timers built into the NextDB.net Form Builder API that allow you to present status to the user while the form is being dynamically built:


	var form = new nextdb.html.InsertForm(conn,"USER");
	form.whileLoading("Loading...");
	form.timeoutMessage("A timeout has occurred...");
	form.render(div);

To learn for about the NextDB.net JavaScript API, check out further docs at: http://www.nextdb.net/docs and many thanks to Brent Hamby for submitting this post.


Viewing latest article 2
Browse Latest Browse All 15

Trending Articles