Alternative 1: Server Push

From Documentation
Alternative 1: Server Push


Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Server push is so-called reverse-Ajax which allows the server to send content to the client actively. With the help of server push, you could send or update the content to the client in the working thread when your predefined condition is satisfied. To use server push is simple,and it requires only three steps as follows,

  1. Enable server push for the desktop
    Invoke Desktop.enableServerPush(boolean bool) to enable server push
  2. Passing components required to be update into the working thread
  3. Invoke the working thread in the desktop

Note: You need to install zkex.jar or zkmax.jar to have the server push, unless you have your own implementation of ServerPush.

Let's take a look at a real example below. If you want to update the number of client using server push, first of all, you have to enable server push for the desktop and then to invoke the thread as follows,

<window title="Demo of Server Push" border="normal">
	<zscript>
	    import test.WorkingThread;    
	    WorkingThread thread;    
	    void startServerpush(){    
		        
	        //enable server push        
	        desktop.enableServerPush(true);        
		        
	        //invoke working thread and passing required component as parameter (in this case we are passing the desktop)            
	        thread= new WorkingThread(desktop);            
	        thread.start();        
	    }       
	</zscript>
	<vbox>
		<button label="Start Server Push" onClick="startServerpush()" />
	</vbox>
</window>

Activation

One thing to notice is that the problem of synchronization which happens when a desktop is accessed by multiple threads at the same time Thus, before accessing the desktop, you have to invoke Executions.activate(Desktop desktop) to get full control of the desktop to avoid this problem, and then release the control of the desktop by invoking Executions.deactivate(Desktop desktop) after the thread finishing its job as follows,

package test;

import org.zkoss.lang.Threads;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.Clients;

public class WorkingThread extends Thread {
   private final Desktop _desktop;

   public WorkingThread(Desktop desktop) {
      _desktop = desktop;
   }

   public void run() {
      try {

         if (_desktop.isServerPushEnabled()) {

            for (int i = 0; i < 5; i++) {
               Executions.activate(_desktop);
               Clients.showBusy("Executing " + (i + 1) + " of 5", true);
               Executions.deactivate(_desktop);
					
               // Simulate a long opetation
               Threads.sleep(2000);
            }
            Executions.activate(_desktop);
            Clients.showBusy(null, false);
            Executions.deactivate(_desktop);
         }

      } catch (InterruptedException ex) {
      } finally {
         _desktop.enableServerPush(false);
      }
   }

}

The result will be something like this:

Behind the Scene

client-polling

The mechanism of server push is implemented using client-polling technique which the client will query the server repetitively to invoke the working thread to do its job, and the frequency of query could be adjusted manually by invoking Executions.setDelay(int min, int max, int factor).

  • min, the minimal delay to poll the server for any pending server-push threads.
  • max, the maximum delay to poll the server for any pending server-push threads.
  • factor, The real delay is the processing time multiplies the delay factor.

Last, one thing to notice is that the frequency will be adjusted automatically depending on the loading of the server

Comet

Since ZK 3.5, in zkmax.jar we provide another mechanism to implement server push. It's called Comet. It's implemented by holding one connection open for real-time events. In particular, the HTTP 1.1 specification states that a browser should not have more than 2 simultaneous connections with a web server.



Last Update : 2011/02/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.