Pregunta

I am displaying a list of items in the web browserthat is initially requested via an AJAX call from the server and then updated by listening to an autobahnjs subscription (which receives change notifications).

Some of my items (a list of job, that sometimes are completed in a few hundred ms, sometimes only after a long time), might be updated at the time the web page is loaded. This is very likely, because the job list is displayed immediatedly after a new job is submitted.

My problem is, that I don't know when the subscription is active, so sometimes changes to my item list occur after I have received the AJAX response but before the subscription becomes active (which results in items never seeing the update).

Starting the subscription ahead of the AJAX call will only partially solve the problem, because now the item list could overwrite a change that occurred earlier via a subscription. Doing so would also require to find out, when the subscription becomes active.

¿Fue útil?

Solución

With an Autobahn based stack (single server node) running WAMP for both RPC and PubSub, one option is like this:

var listInitialized = false;

session.subscribe("http://myapp.com/onListChange",
   function (topic, event) {
      if (listInitialized) {
         // update list
      }
   }
});

session.call("http://myapp.com/getList").then(
   function (res) {
      // initial fill of list
      listInitialized = true;
   }
);

Since both the subscribe and the call run over a single WAMP connection (and that connection is an ordered WebSocket/TCP transport), the Autobahn server will setup the subscription and deliver events before the call is even processed. That subscription might yield events that arrive before the call returns, but these are ignored via the flag. Later, when the call returns and result has been processed, subsequent events are processed by the client.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top