Salesforce Streaming API Recconnect to Cometd after session expires

0 votes

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?

Mar 16, 2022 in SalesForce by surbhi
• 3,820 points
727 views

1 answer to this question.

0 votes

I'm responding from the perspective of CometD.

Doing nothing is the best approach to restart a CometD session after the previous one has expired.

CometD offers an automatic reconnect option, which means that if the network connection stops and the session expires, CometD will retry until it connects to the server again, with a backoff delay between attempts.

You only need to follow the recommendations to ensure that your subscriptions are restored on re-handshake, in particular, perform your subscriptions from a /meta/handshake successful listener.


You must manually re-handshake if you explicitly disconnect the client:

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);

Hope this helps!

Enroll in the Salesforce Course and learn more!

answered Mar 17, 2022 by CoolCoder
• 4,420 points

Related Questions In SalesForce

0 votes
0 answers

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

In logic app i'm using salesforce connector after deployment ...READ MORE

Mar 1, 2022 in SalesForce by surbhi
• 3,820 points
1,036 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

you are not passing the parameterValues, use ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
942 views
0 votes
1 answer

Salesforce - trigger auto-response rule after creating case through API

Less complexity, on the other hand, generally ...READ MORE

answered Apr 1, 2022 in SalesForce by CoolCoder
• 4,420 points
1,560 views
0 votes
0 answers

Salesforce API: How to identify a Case from an email reference code ("[Ref: ... :Ref]")?

1 I'm writing a Windows service that will ...READ MORE

Apr 5, 2022 in SalesForce by surbhi
• 3,820 points
770 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,342 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,017 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
710 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 707 views
0 votes
1 answer

Cannot login to Salesforce Sandbox via python API

Set domain='test' and generate a new token ...READ MORE

answered Mar 1, 2022 in SalesForce by CoolCoder
• 4,420 points
1,819 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

You are not passing the parameterValues, use ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,420 points
776 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP