4

I am trying to use CometD with my platform events. I've added a basic use case for the time being for clarity: * I have a platformevent and a trigger. Whenever a client (custom object) gets edited, a trigger publishes the event on the Event bus using the .publish method. I then want one of my visual force pages to subscribe to this event and display a pop up.

I've created a test dummy visual force page and that page can successfully subscribe to the event and the pop up gets displayed when the client has been edited however when I try and implement it on the page I want to, it doesn't seem to subscribe. The visualforce page currently consists of a component.

<apex:page standardStylesheets="false" sidebar="false" showHeader="false" >

<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery_mini_1_6_4}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>

<style>
/* The snackbar - position it at the bottom and in the middle of the screen */
#snackbar {
visibility: hidden; /* Hidden by default. Visible on click */
min-width: 250px; /* Set a default minimum width */
margin-left: -125px; /* Divide value of min-width by 2 */
background-color: #333; /* Black background color */
color: #fff; /* White text color */
text-align: center; /* Centered text */
border-radius: 2px; /* Rounded borders */
padding: 16px; /* Padding */
position: fixed; /* Sit on top of the screen */
z-index: 1; /* Add a z-index if needed */
left: 50%; /* Center the snackbar */
bottom: 30px; /* 30px from the bottom */
}

/* Show the snackbar when clicking on a button (class added with JavaScript) */
#snackbar.show {
    visibility: visible; /* Show the snackbar */
    /* Add animation: Take 0.5 seconds to fade in and out the snackbar. 
   However, delay the fade out process for 2.5 seconds */
   -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
   animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

/* Animations to fade the snackbar in and out */
@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 0; opacity: 0;}
}
</style>

<script>

$(document).ready(function () {
            $.cometd.init({
                url: window.location.protocol + '//' + window.location.hostname + '/cometd/41.0/',
                requestHeaders: {Authorization: 'OAuth {!$Api.Session_ID}'}
            });
            console.log('here'+$.cometd);
            $.cometd.subscribe('/event/EventPop__e', function (message) {
                console.log('in subscribe');                   
            });
        });


function myFunction() {
// Get the snackbar DIV
var x = document.getElementById("snackbar");

// Add the "show" class to DIV
x.className = "show";

// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
</script>

/*...Component here...*/

<br/>

<div id="lightning" />

<button onclick="myFunction()">Show Snackbar</button>

<!-- The actual snackbar -->
<div id="snackbar">Some text some message..</div>

I added the console.log message in the subscribe method for testing and that message does not get displayed in the console whenever a client is updated.

  • I would suggest before testing your streaming API in VF page first check notification in Workbench > Queries > Streaming Push Topic. – Prasanta Kumar Pardhi Infosys Oct 10 '18 at 12:01
  • 1
    Please also have a look at the CometD subscribe() documentation: the subscribe() call should be done from within a handshake callback, when the handshake reply message is successful. – sbordet Oct 10 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.