function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raguraman Selvaraj 5Raguraman Selvaraj 5 

WebSocket connection to 'wss://awr--raguramans--c.visualforce.com/cometd/49.0' failed: Error during WebSocket handshake: Unexpected response code: 405

Hi Everyone,

I am getting socket error, while trying to subscribe platform event through Visual force page. Kindly assist me on this.

<apex:page > <!-- Begin Default Content REMOVE THIS --> <h1>Congratulations</h1> This is your new Page <!-- End Default Content REMOVE THIS --> <div id="content"> </div> <apex:includeScript value="{!$Resource.cometd}"/> <apex:includeScript value="{!$Resource.jQuery}"/> <apex:includeScript value="{!$Resource.json2}"/> <apex:includeScript value="{!$Resource.jQuery_cometd}"/> <script type="text/javascript"> (function($){ $(document).ready(function() { $.cometd.configure({ url: window.location.protocol+'//'+window.location.hostname+'/cometd/49.0', requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'} }); console.log('before handshake'); $.cometd.handshake(); console.log('before addListener'); $.cometd.addListener('/meta/handshake', function(message) { console.log('before subscribe'); $.cometd.subscribe('/event/Event_On_Leads__e', function(message) { console.log('testing subscribed'); var div = document.getElementById('content'); div.innerHTML = div.innerHTML + '<p>Notification </p><br/>' + 'Streaming Message ' + JSON.stringify(message) + '</p><br>'; }); }); }); })(jQuery) </script> </apex:page>
ShirishaShirisha (Salesforce Developers) 
Hi Raguraman,

Greetings!

Can you please check this thread (https://stackoverflow.com/questions/49575350/websocket-connection-to-wss-error-during-websocket-handshake-unexpected-re) which will help you in troubleshooting the handshake failure issues.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Raguraman Selvaraj 5Raguraman Selvaraj 5
Hi Shirisha Pathuri,

Thanks for your quick response.

Looks the error code given in the link is different (I am getting 405 error code but this link shows as 400). I am not sure the given link will help me, however deeply trying with given link. Please refer the below given details and assist me further to fix this issue.

405 error states that "Method Not Allowed". 
400 error states that "Bad Request". 

I am getting error in the below mentioned line, when I went and check the browser console.
  context.webSocket = protocol ? new window.WebSocket(url, protocol) : new window.WebSocket(url);
  Error Message: WebSocket connection to 'wss://awr--raguramans--c.visualforce.com/cometd/49.0' failed: Error during WebSocket handshake: Unexpected response code: 405
 
This error occurs at visual force line:  $.cometd.subscribe('/event/Event_On_Leads__e', function(message) {}
Raguraman Selvaraj 5Raguraman Selvaraj 5
Hi Shirisha,

My issue got solved after added  $.cometd.websocketEnabled = false; statement in the below visual page code.

apex:page >
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <!-- End Default Content REMOVE THIS --> <div id="content">
        
    </div>
    <apex:includeScript value="{!$Resource.cometd}"/>
    <apex:includeScript value="{!$Resource.jQuery}"/>
    <apex:includeScript value="{!$Resource.json2}"/>
    <apex:includeScript value="{!$Resource.jQuery_cometd}"/>
  
    
    <script type="text/javascript">
   
    (function($){
        $(document).ready(function() {
        
     $.cometd.configure({
             url:  window.location.protocol+'//'+window.location.hostname+'/cometd/49.0',
             requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'},
        appendMessageTypeToURL : false
      });     
    console.log('before handshake')  
    $.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');
                  
                });   
                });
            
        });
    })(jQuery)
    </script>
</apex:page>