Twitter Logo Follow us on Twitter
Project Information About this project

Server Push

Sometimes it's necessary to “push” UI updates from the RAP server to the RAP client. As an example, imagine a chat application that notifies the user of a chat request from a peer.

HTTP is not designed to actively push data to the client. A RAP server can only answer requests from a client, but it cannot actively send any message to the client. In RAP the UI is typically updated within a request caused by a user interaction, in which case this restriction is not an issue. However, when the UI is updated from a background thread, there's usually no request that could fetch the UI updates from the server. In this case the UI changes would not be rendered until the next HTTP request is triggered.

The Solution

Using the so-called Comet (or “long polling”) approach, the server is able to notify the client immediately. The basic idea is that the client sends a request to the server that is not answered until the server needs to notify the client of a change.

RAP provides an implementation of this mechanism which can be used via the ServerPushSession API. Before starting a background task, create an instance of ServerPushSession and call start. When the background task has completed and no longer needs the server push mechanism, call stop. Example:

final ServerPushSession pushSession = new ServerPushSession();
Runnable bgRunnable = new Runnable() {
  public void run() {
  // do some background work ...
  // schedule the UI update
  display.asyncExec( new Runnable() {
    public void run() {
      if( !widget.isDisposed() ) {
        // update the UI
        label.setText( "updated" );
        // close push session when finished
  } );
Thread bgThread = new Thread( bgRunnable );
bgThread.setDaemon( true );

A UI session may create and start any number of ServerPushSessions in parallel. The framework will keep the server push enabled as long as there are any active push sessions. That is, for multiple push sessions, there is only a single request needed.

Note that unlike Display#asyncExec(), Display#timerExec(), does not require an explicit ServerPushSession, as RAP creates one automatically in this case.

More technical details of the ServerPush implementation can be found on the RAP Wiki.