ItsNat v0.6 released. Comet (long-polling) Now Event-Based

by Jose Maria ArranzMarch 25th, 2009

ItsNat version 0.6 has been released, and adds a new event system for Comet using long-polling. When the client is going to be notified that something occurred in the server (the method CometNotifier.notifyClient() has been called), a special Comet DOM event (an extension of W3C DOM) is fired and dispatched to the optional event listener registered on the CometNotifier.

The thread executing the event listener is a web-request thread and in this context the document (ItsNatDocument) is locked by this thread, so there is no need for synchronization. This is an opportunity to change the server DOM, to send custom JavaScript, etc. Prior to this release, the background thread usually updated the client (synchronizing the document first). Now, the event listener approach is preferred, but the old approach is still valid.

The following example shows how a background thread notifies the client every 2 seconds. There are two notifications (two alerts), the first one pushed by the background thread and second alert is pushed by the event listener. Of course the method CometNotifier.notifyClient() needs to be called by the background thread, otherwise the first alert is queued but not sent and the Comet event is not fired and dispatched.

    final ItsNatDocument itsNatDoc = ...;
 
    final CometNotifier notifier =
        itsNatDoc.getClientDocumentOwner().createCometNotifier();
    EventListener listener = new EventListener()
    {
        public void handleEvent(Event evt)
        {
           itsNatDoc.addCodeToSend("alert('Tick From Event');");
        }
    };
    notifier.addEventListener(listener);
 
    Thread backgroundThr = new Thread()
    {
        public void run()
        {
            System.out.println("Background server task started");
            long t1 = System.currentTimeMillis();
            long t2 = t1;
            do
            {
                try
                {
                    Thread.sleep(2000);
                }
                catch(InterruptedException ex) { }
 
                if (!notifier.isStopped())
                {
                    synchronized(itsNatDoc)
                    {
                       itsNatDoc.addCodeToSend("alert('Tick From Thread');");
                    }
                    notifier.notifyClient();
                }
                t2 = System.currentTimeMillis();
            }
            while( (t2 - t1) < 10*60*1000 ); // Max 10 minutes
 
            notifier.stop();
 
            System.out.println("Background server task finished");
        }
    };
 
    backgroundThr.start();

Another interesting feature introduced in ItsNat v0.6 is a draft of the full remote control capability. In full remote control, two or more users can “share” the same web page, that is, if a user does something and the page changes, the page of other users are updated accordingly. Polling and long-polling can be easily used to update the page the users attached to the original page (if the original page need to be updated automatically an Ajax timer or CometNotifier can also be used). In previous versions, only read-only monitoring was possible (”remote views”).

The full remote control capability is disabled by default, and must be enabled by using an internal flag, if you need this feature now contact with me to learn how to enable it.

One Response to “ItsNat v0.6 released. Comet (long-polling) Now Event-Based”

  1. Comet Daily » Blog Archive » Comet to Follow Server Threads Progression Says:

    [...] (not a web thread) executing a long task in parallel. This article is “inspired” on this musing published here at Comet Daily and this complete [...]


Copyright 2015 Comet Daily, LLC. All Rights Reserved