What's the best approach to restart a cometd session after it's been idle for a while? When using the CometD Javascript Library and the Salesforce Streaming API, this is especially true.
I'm currently working on a set of Web Pages that use the Salesforce Streaming API, which runs on Cometd, to update data on the page automatically. This is going good, but I'm having trouble figuring out what to do when the session ends.
I'm now testing in a very short time frame, maybe 20 seconds. The code below is called twice. The first time is when the page loads, and the second time is 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 fine, but there are a few issues that I'd like to prevent.
My subscriptions handlers appear to fire twice on the next call it captures if I receive a call through my subscriptions before the code above executes the second time. I tried substituting $.cometd.init with $.cometd.handshake, but it only worked if one of my subscriptions was called before the code above was called again. I also tried calling $.cometd.disconnect, but even if I ran it synchronously, it seemed to prevent any calls from reaching my subscribers after I called that method.
Is it possible that I'm doing everything correctly and the 20 second interval is causing problems, or is there a better way to go about it?