Wednesday, April 4, 2012

Asynchronous submissions

You might not have heard about it, but XForms supports asynchronous submissions since version 1.1. In fact, XForms 1.1 even made the asynchronous behavior the default, probably because in the browser XMLHttpRequest is asynchronous. [1]

So what does it mean for an XForms submission to be asynchronous? Simply that sending the submission (with an action or the <submit> control) doesn't wait for the submission to be fully completed before returning. For example, consider:


And then this sequence of actions:
<send submission="save">
<setvalue ref="foo" value="42"/>
The <send> action starts the submission with an xforms-submit event, instance data is prepared, and then an HTTP request is started with that data as payload. "Soon" after, control returns to the action processing, and the <setvalue> action runs. The response from the submission may not have yet happened at that point. In fact, if the submission takes a long time (say, seconds or minutes), the submission will just be hanging there, waiting for a response. Once the response arrives, the submission result is processed.

With a synchronous submission, on the other hand, the <setvalue> action only runs once the response has arrived (or once an error has occurred). So the submission mode matters when you deal with XForms actions and events, and you have to be aware of the difference in processing between the two types of submissions.

There are useful practical uses to asynchronous submissions, including:
  • running background operations, for example autosaving data
  • reducing latency, for example loading initial data from multiple services concurrently
Background operations are good because they allow the user to continue working with the application while the operation completes. Reducing latency is good for the user because pages load faster.

Now since Orbeon Forms implements submissions on the server, you might wonder how this works! Orbeon Forms checks for pending asynchronous submissions at the end of page initialization and at the start and end of an Ajax request.

If page initialization or an Ajax request terminates and submission are still pending, the browser is instructed to poll the server from time to time, to check whether pending submissions have completed. Those which have are then handled appropriately.

You can find more details in the documentation on asynchronous submissions.

[1] Orbeon Forms has so far kept the default to synchronous for backward compatibility reasons.

No comments:

Post a Comment