I am trying to subscribe to a platform event using cometd. I have asserted that the event is getting published just fine but its subscribing that is an issue.
I have placed console logs in the cometd.js file and found out that when it subscribes, it logs out unsuccessful on the _notifylisteners method as the channel is gets passed is meta/unsuccessful
.
Anyone have any suggestions? This is how im using cometd in in javascript:
$.cometd.init({
url: window.location.protocol + '//' + window.location.hostname + '/cometd/44.0/',
requestHeaders: {Authorization: 'OAuth {!$Api.Session_ID}'}
});
$.cometd.subscribe('/event/EPlatformEvent__e', function (message)
{
console.log('Worked');
});
Upon further debugging, this is what I am getting in the message
object that is being passed:
{advice: {…}, channel: "/meta/subscribe", id: "8", error: "403::Unknown client", successful: false}
Edit:
So after researching more into this issue, there were a few suggestions suggesting that the subscribe might be happening before the handshake therefore it's wise to add some sort of delay. I've got the following code and it now does connect to the platform event as it gives the following in the console logs:
{clientId: "13qcqfozc84hy0n1hmd3u5jscryk", channel: "/meta/subscribe", id: "29", subscription: "/event/EPlatformEvent__e", successful: true}
but still does not output the code that's supposed to get outputted when an event is published. This is my code now:
(function ($) {
$(document).ready(function () {
$.cometd.init({
url: window.location.protocol + '//' + window.location.hostname + '/cometd/44.0/',
requestHeaders: {Authorization: 'OAuth {!$Api.Session_ID}'}
});
setTimeout( function(){console.log('waiting..');}, 5000);
$.cometd.addListener('/meta/unsuccessful', metaUnSucessfulListener);
//$.cometd.subscribe('/event/EPlatformEvent__e', function (message) {
// console.log('working!');
//});
try{
$.cometd.subscribe('/event/EPlatformEvent__e', function (message) {
console.log('wor33king!');
});
}catch(e){
console.log('catch block subscribe');
}
});
})(jQuery)
function metaUnSucessfulListener(message) {
console.log(
'DEBUG: /meta/unsuccessful Error: ' +
JSON.stringify(message) + ' <br>');
unsubscribe();
setTimeout( function(){
console.log('init of pt failed');
$.cometd.init({
url: window.location.protocol + '//' + window.location.hostname + '/cometd/44.0/',
requestHeaders: {Authorization: 'OAuth {!$Api.Session_ID}'}
});
$.cometd.addListener('/meta/unsuccessful', metaUnSucessfulListener);
$.cometd.subscribe('/event/EPlatformEvent__e', function (message) {
console.log('wor22king!');
});
}, 5000);
};
function unsubscribee() {
$.cometd.unsubscribe('/event/EPlatformEvent__e');
$.cometd.disconnect();
}
function subscribee(){
console.log('subscribing...');
$.cometd.subscribe('/event/EPlatformEvent__e', function (message) {
console.log('working!');
});
}
function unsubscribe() {
console.log('in the unsub');
$.cometd.unsubscribe('/event/EPlatformEvent__e');
$.cometd.disconnect(true);
}
__e
in the event name? Shouldn't it be as/event/EPlatformEvent__e
?