Progress bars with jQuery UI, WebSocket & Play
This article shows you how to use a jQuery UI component and Web Sockets to implement a progress bar for a deterministic asynchronous server-side job in a Play framework web application. This is an example of a user-interface component that is not part of the web’s stateless request-response paradigm, but is now possible with new HTML5 features.
This follows on from earlier jQuery UI component articles: jQuery UI Ajax autocomplete with Play , Lazy loading page content with jQuery UI and Selector dialogue with jQuery UI . The examples and source code for all of these articles are available as part of the Play framework jQuery UI module .
For this example, we are going to start a long-running job in our web application on the server, and display progress in real-time:
Note that this time we are not using a time zones example, although the recent time zone database debacle seems to have been largely resolved .
Architecture
The Architecture of the World Wide Web is about resources and their identifiers and representations, and while we are used to the original web of HTML documents and URLs, resources and representations can be richer. The key resource in this progress bar example is a server-side process.
The Play framework includes support for starting asynchronous server-side jobs - processes that continue running outside the scope of the HTTP request-response cycle. This is useful for important tasks that must be completed independently of the user’s navigation behaviour, such as sending hundreds of party invitation e-mails. The challenge with these long-running jobs is to use URIs to identify them as ‘resources’ in the web architecture sense, and to provide useful representations.
For this example, we will use a web socket URI to address an asynchronous job, and on the server-side generate a representation consisting of only a single number - the job’s completion percentage. Play’s web sockets support is ideal for updating the progress bar, and Play’s asynchronous jobs are suitable for long-running jobs.
Note that web sockets support on both server-side and client-side is still evolving. This example works with Play 1.2.3 and Safari 5.1.1; Play 1.2.4 adds Chrome support.
Conclusion
Web sockets are an HTML5 technology that you can now use in your web applications, thanks to browser support for the JavaScript API and Play’s server-side support. This is good news for developers who can see opportunities to improve user-experience in their applications with more sophisticated applications.
is a senior software developer at Lunatech Research and committer on the Play open-source project._