Question

What is the best way to restart a cometd session after the old one has expired? Specifically when using the CometD Javascript Library and Salesforce Streaming API.

Currently I'm trying to write a series of Web Pages that use the Salesforce Streaming API,which runs using Cometd, to automatically update data on the page. This is working fine, but I'm now trying to deal with what to do after the session expires.

Currently I'm testing in a very small time frame, specifically 20 seconds. I call the code below twice. First when the page loads and again after 20 seconds.

$.cometd.clearListeners();
$.cometd.init({
                url:window.location.protocol+'//'+window.location.hostname+'/cometd/28.0/',
                requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
            });
            [Initialize Subscriptions]

This works okay, but not without some problems that I'd like to avoid.

It looks like if I receive a call through my subscriptions before the code above runs the 2nd time, my subscriptions handlers fire twice on the next call it catches. I've tried replacing $.cometd.init with $.cometd.handshake, but this never seemed to work unless my subscriptions received a call before the code above was called a 2nd time. I also tried calling $.cometd.disconnect, but this seemed to block any calls from reaching my subscriptions after I call that function, even if I run it synchronously.

Am I doing it right and the 20 second interval is messing things up or is there a better way to do this?

Was it helpful?

Solution

I am answering from the CometD point of view.

The best way to restart a CometD session after the old one expired is to do nothing.

CometD has an automatic reconnect feature so if the network connection drops and the session expires, CometD will automatically retry until it succeeds to connect again with the server, with a backoff interval between attempts.

In order to guarantee that your subscriptions are restored on re-handshake, you just need to follow the guidelines, in particular perform your subscriptions from a /meta/handshake successful listener.

If you want to test your JavaScript code behaviour in case of a disconnection, you just need to create the disconnection, and not calling cometd.init() twice. You can kill the server, unplug the network cable, undeploy the webapp, etc.

If you explicitly disconnect the client, then you have to re-handshake manually:

cometd.init(...);
...

// Disconnect
cometd.disconnect();
// Wait a while, then re-handshake
cometd.handshake();

Note that you must "wait a while" because cometd.disconnect() is asynchronous. Alternatively, you can do:

cometd.init(...);
...

// Disconnect and re-handshake when fully disconnected
cometd.addListener('/meta/disconnect', function(message) 
{ 
    cometd.handshake(); 
});
cometd.disconnect();

// Another solution, using setTimeout()
cometd.disconnect();
window.setTimeout(function() 
{ 
    cometd.handshake(); 
}, 5000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top