Diff for "LongPoll/JavaScript"

Not logged in - Log In / Register

Differences between revisions 2 and 3
Revision 2 as of 2011-10-05 10:11:56
Size: 3344
Editor: rvb
Comment:
Revision 3 as of 2011-10-05 10:12:43
Size: 3344
Editor: rvb
Comment:
Deletions are marked like this. Additions are marked like this.
Line 62: Line 62:
  fired each time a long poll transaction fails. This might be because   fired each time a long-poll transaction fails. This might be because
Line 66: Line 66:
  To avoid hammering the server in case of an outage, the Long poll
  Javascript library waits for 1 second before reconnecting. Also,
  To avoid hammering the server in case of an outage, the long-poll
  JavaScript library waits for 1 second before reconnecting. Also,
Line 70: Line 70:
  end user aware of the connection status.   end user aware of the connection status:
Line 86: Line 86:
Since all the long poll Javascript library will be doing is firing
Javascript events, testing a Javascript application that interacts
Since all the long-poll JavaScript library will be doing is firing
JavaScript events, testing a JavaScript application that interacts
Rendering of reStructured text is not possible, please install Docutils.

===========================
LongPoll JavaScript library
===========================

The `long-poll Javascript library`_ is responsible for connecting to
`lp:txlongpoll`_ and firing Javascript events to reflect the state
of this connection and make RabbitMQ events available to the
JavaScript layer.

.. _`long poll Javascript library`: http://bazaar.launchpad.net/~launchpad-pqm/launchpad/devel/view/head:/lib/lp/app/longpoll/javascript/longpoll.js

.. _lp:txlongpoll: https://launchpad.net/txlongpoll

Connection
----------

The long-poll library will issue a connection to the long-poll server
iff LP.cache.longpoll.uri and LP.cache.longpoll.key are populated in
the JSON cache.  After each successful or failed connection, the long
poll library reconnects to the long poll server to be able to handle
new events.

`Request Timeout errors`_ are expected and are ignored by the long-poll JavaScript code (i.e. a new request is started when such an error happens).

.. _`Request Timeout errors`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9

Events
------

Two kind of events are fired by the long-poll Javascript
library. Events deriving from LongPollEvents and "system" events
indicating the state of the long-polling connection. Here is a list of
all the possible events fired by the Javascript LongPoll library:

*event_key*
  fired each time RabbitMQ passes an event created using the
  LongPollEvent adapter (event_key being the name of the RabbitMQ
  event).

  For instance, this in LP python code::

   # subscription code omitted.
   class FakeEvent(LongPollEvent):

       implements(ILongPollEvent)

       @property
       def event_key(self):
          return "event-key-test"

   event = FakeEvent("source")
   event_data = {"hello": 1234}
   event.emit(**event_data)

  ... will result in an event being fired on the Javascript side::

   Y.fire("event-key-test", {"hello": 1234});

*'lp.app.longpoll.failure'*
  fired each time a long-poll transaction fails. This might be because
  of connection problems or because the library failed to parse the
  event payload.

  To avoid hammering the server in case of an outage, the long-poll
  JavaScript library waits for 1 second before reconnecting.  Also,
  after 5 failed connections, the library will wait 3 minutes before
  trying to reconnect. The following events can be used to make the
  end user aware of the connection status:


*'lp.app.longpoll.longdelay'*
  fired each time the library switches in 'longdelay' mode (i.e. after
  5 failed connections).


*'lp.app.longpoll.shortdelay'*
  fired each time the library switches back in 'shortdelay' mode
  (i.e. after at least 5 failed connections and then after a
  successful connection).

Testing
-------

Since all the long-poll JavaScript library will be doing is firing
JavaScript events, testing a JavaScript application that interacts
with this library is pretty straightforward. Simply fire the events
and make sure the application reacts appropriately::

 // Fire "event-key-test".
 Y.fire("event-key-test", {"hello": 1234});

 // Simulate a failure.
 Y.fire("lp.app.longpoll.failure");

 // Simulate the library going into 'longdelay' mode.
 Y.fire("lp.app.longpoll.longdelay");

LongPoll/JavaScript (last edited 2011-10-05 10:14:52 by rvb)