[aida] Comet with Aida/Web (was How to force an Application to refresh its view)

Janko Mivšek janko.mivsek at eranova.si
Wed Feb 6 23:57:57 CET 2008


Hi Rob,

Well, this one is actually most advanced topic of web application 
building in general and it is called Comet.

Comet, or so called Reverse Ajax is an updating some element of your web 
page when some event occurs on the server side, not the client one.

In Aida this is easy achievable by deferring a response to periodic Ajax
update of some element until a server event occurs. And to signal that 
event, you can use Announcements, just as you do.

Basically you do:

1. make some element for a content to be Comet updatable, in some
    method, say #liveContent.
2. let you Ajax periodicaly update that element, but with very long
    period:

	e add: (self liveContent updateEverySeconds: 1000)

So far so good, no let we prepare our reverse Ajax by deferring the 
response. Let we now break that delay and respond immediately when some 
event is sent via Announcements framework

4. let we prepare our defer code at the start of #liveContent

MyApp>>liveContent
   self session lastRequest isAjaxRequest ifTrue:
	[self waitForAnyChange].
   e := WebElement new.
   ....
   ^e

MyApp>>waitForAnyChange
  | count change |
  count := 1. change := false.
  self observee
      when: OurAnnouncement do: [:ann | change := true] for: self.
  [change not and: [count < 1000]] whileTrue:
     [(Delay forSeconds: 1) wait.
     count := count + 1].
  self observee unsubscribe: self

OurAnnouncement is a subclass of Announcement for our case. Above code 
will respond in a less that 1 second after an event occurs. If you want 
faster response, change Delay code accordingly. Also this method can be 
possibly done better...

5. Domain object (observe) must now send OurAnnouncement on event to 
trigger sending above response. Look at Announcements docs for that. 
Probably you'll in myDomainObject just do:

	self announce: OurAnnouncement.

I hope that helps a bit

Best regards
Janko






Stefan Schmiedl wrote:
 > On Wed, 6 Feb 2008 16:48:44 -0500
 > "Rob Rothwell" <r.j.rothwell na gmail.com> wrote:
 >
 >> In other words the browser only refreshes itself when it wants to!
 >
 > ... unless you specify a refresh meta tag, for example, which does for
 > the whole page what I assume updateEverySeconds: does for a single
 > element.
 >
 >> If you don't use special "server push" technology, another way of
 >>> achieving a similar effect would be to periodically query the
 >>> application for status updates ... AJAX is used for stuff like this.
 >>
 >> So...would you just recommend using one of the AJAX methods like
 >> "updateEverySeconds:" to poll for changes?  I was actually trying to 
write a
 >> simple little department "chat" program, and the AJAX way works, but
 >> checking the server every few seconds just seemed like a bit much.
 >
 > wellllllll... google has a "special" search dialog somewhere (probably
 > in "beta" :-) which polls the server after every keystroke recorded by
 > the input box. If you type slow enough, you can watch suggestions for
 > search terms pop up.
 >
 >> Can I create a new type of component that simply ask the server for an
 >> update "on demand" rather then periodically?  I am getting a message 
and all
 >> the information I need is in my domain model...
 >
 > Define "on demand". Who is the "I" getting the message. I assume that
 > the "I" owning "my domain model" is the application server.
 >
 >> Maybe what I'm asking for is already part of the AIDA AJAX elements 
and I
 >> just don't know how to use it...
 >>
 >
 > I'll defer to people actually knowing AIDA on this one :-)
 >
 > s.


-- 
Janko Mivšek
AIDA/Web
Smalltalk Web Application Server
http://www.aidaweb.si


More information about the Aida mailing list