3

I am trying to subscribe to platform events on VF page. But getting WebSocket connection error. Not sure what am I doing wrong.

enter image description here

<apex:includeScript value="{!URLFOR($Resource.cometd, '/newCometd/cometd.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.scripts, '/scripts/jquery.min.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.cometd, '/newCometd/json2.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.cometd, '/newCometd/jquery.cometd.js')}"/>


    (function($){
      $(document).ready(function() {
          // Connect to the CometD endpoint
          $.cometd.init({
             url: window.location.protocol+'//'+window.location.hostname+'/cometd/44.0/',
             requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
         });

         // Subscribe to a topic. JSON-encoded update will be returned
         // in the callback
         $.cometd.subscribe('/event/Upload_Notification__e', function(message) {
             //callRefresh();
          });
      });
   })(jQuery)

Thanks in advance

1 Answer 1

1

I had the same problem. This was recently resolved here.

The resolution (copied from that link) involves splitting the init() call into separate configure() and handshake() calls so that websocketEnabled can be disabled.

Note: I was also getting an appendMessageTypeToURL error which the following code fixes by adding an additional requestHeader.

$.cometd.configure({
     url: window.location.protocol+'//'+window.location.hostname+'/cometd/49.0',
     requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'},
                     appendMessageTypeToURL : false
     });
 
console.log('before handshake');

// disable websockets
$.cometd.websocketEnabled = false; 
      
$.cometd.handshake();

console.log('before addListener');  
    
$.cometd.addListener('/meta/handshake', 
                     function(message) {
                         $.cometd.subscribe('/event/Event_On_Leads__e', 
                                            function(message) {                    
                                                console.log('testing subscribed');
                                            }
                         );   
                     });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .