I am trying to build a Lightning component by an example from Salesforce Developers Youtube channel, here is original video: https://youtu.be/G931jWBnxHU?t=42m26s
So author encaplulated code for subscribing for Platrorm events in custom component.
Usage:
<c:Subscriber eventName="MyEvent__e" onmessage="{!c.onMessage}" />
Component:
<aura:component description="Subscriber" controller="SessionController">
<aura:attribute name="eventName" type="String" />
<ltng:require scripts="{!$Resource.cometd + '/cometd-javascript-master/cometd.js'}" afterScriptsLoaded="{!c.onJSLoaded}" />
<aura:registerEvent name="onmessage" type="c:MessageEvent" />
</aura:component>
Controller:
({
onJSLoaded: function(component, event, helper) {
debugger;
var action = component.get("c.getSessionId");
action.setCallback(this, function(response) {
var sessionId = response.getReturnValue();
var cometd = new window.org.cometd.CometD();
cometd.configure({
url: window.location.protocol + '//' + window.location.hostname + '/cometd/40.0',
requestHeaders: { Authorization: 'OAuth ' + sessionId }
});
cometd.handshake($A.getCallback(function(status) {
if (status.successful) {
var eventName = component.get("v.eventName");
cometd.subscribe('/event/' + eventName, $A.getCallback(
function(message) {
console.log(message);
var messageEvent = component.getEvent("onmessage");
messageEvent.setParam("message", message);
messageEvent.fire();
}
));
}
}));
});
$A.enqueueAction(action);
}
})
When I place my component in browser, it fails at handshake method with an error
VM57302:161 Refused to connect to 'wss://mydomain-dev-ed.lightning.force.com/cometd/40.0' because it violates the following Content Security Policy directive: "connect-src 'self' *.eu5.visual.force.com https://api.bluetail.salesforce.com https://preprod.bluetail.salesforce.com https://eu5.salesforce.com https://staging.bluetail.salesforce.com".
I found this article: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_csp_stricter.htm
I deactivated "Enable Stricter Content Security Policy for Lightning Components" critical update but that did not help.
EDIT 1
Added line cometd.websocketEnabled = false;
to cometd condiguration code, now handshake is not blocked by browser, but fails with error ypeError: Cannot add property context, object is not extensible↵ at F.LongPollingTransport._self.xhrSend (/resource/1502195543000/cometd/cometd-javascript-master/cometd.js:564:25)...