The Salesforce Streaming API is a wonderful tool that can help you create UIs that stay up to date with your data in real time. You also might find that it mysteriously doesn’t work in IE11, which Salesforce still officially supports on the platform.
Problem 1: Salesforce documentation is horribly out of date
The official documentation has you installing CometD version 2.2 (from 2011) against Salesforce API version 24. This version is way to old to work with IE. I found a blog post from 2014 that determined you need at least 2.9
Problem 2: CometD folder structure has changed
- jQuery is no longer included in the CometD download. Grab the latest jQuery from jquery.com. You need at least 3.1.0 but I used 3.2.1
- json2.js is no longer included in the CometD download. Theoretically this is no longer needed as it is only for IE8 and below and SF only supports IE11 so ignore it.
Problem 3: CometD defaults to WebSockets
Salesforce doesn’t support WebSockets. CometD is supposed to detect this failure and fall back to https, however, it does so by attampting the WebSocket connection, catching the failure, and re-trying with https. Very inefficient and also means that the example code from the Streaming API Developer Guide will not work as is. There are two fixes to make sure you have a robust connection, only one is required but the combination builds in some efficiency and reliability so I highly reccoment implementing both fixes.
Add the line
before calling $.cometd.init to disable WebSockets right off the bat. This will prevent CometD from even attempting the pointless WebSockets call.
Do your subscribes only after the handshake has succeeded by using callbacks instead of syncronous calls like the Salesforce documented example has you do. To do this, you need to split the legacy init call into configure and handshake with a callback.
- $.comet.init should simply change to $.cometd.configure, all parameters can stay exactly as they are.
- Wrap your subscribe call the handshake callback
Problem 4: VisualForce overrides XMLHttpRequest with IServerXMLHTTPRequest2 in IE only
This is a problem because IServerXMLHTTPRequest2 is missing attributes used by the jQuery $.ajax calls.
This one took me forever to find as I had to first use IE debugger with breakpoints to even find out that the object substitution was taking place and then IServerXMLHTTPRequest2 + anything in a search engine returns no results. Finally, I found this forum post. Luckily the fix is easy, just add the following code block before making any $.CometD calls
Putting it all together
Lets take the Salesforce example
This would become
I hope this saves some of you some huge headaches when using the Streaming API with IE11.