3

General question as the documentation is very limited but how do I subscribe to a Platform event within my visualforce page / controller? My platform event is set up like below I have already managed to get the publisher working.

enter image description here

1

Platform events are built on same technology that we were using in Push topics.

You can subscribe to platform event similar to how we used to subscribe to pushtopics. The only difference is the channel name. Here is the format of the platform event topic (channel) name:

/event/__e

Like in your case url will be /event/ILOSRecording_event__e

    <apex:page >
         <apex:includeScript value=”{!$Resource.cometd}”/>
<apex:includeScript value=”{!$Resource.jquery}”/>
<apex:includeScript value=”{!$Resource.json2}”/>
<apex:includeScript value=”{!$Resource.jquery_cometd}”/>

        <script>
                            cometd.configure({
                   url: 'https://<Salesforce_URL>/cometd/41.0/',
                   requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
        });


                            $.cometd.subscribe('/event/ILOSRecording_event__e', function(message)  
                            {  //your logic for whenever event is fired.
                                });

        </script>
    </apex:page>

for static resouces for cometd click on := https://developer.salesforce.com/page/Streaming_Real_Time_Data_into_Visualforce

for more on platform events click on https://trailhead.salesforce.com/modules/platform_events_basics/units/platform_events_subscribe

4

I've tried the code snipped mentioned above but ran into a few errors.

I made a the following change and got it to work.

cometd.configure() --> $.cometd.init()

Thought I'd share if anyone else is having trouble.

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

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

<script>    

$.cometd.init({
    url:  window.location.protocol+'//'+window.location.hostname+'/cometd/41.0/',
    requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
});        

$.cometd.subscribe('/event/<EventName>', function(message) {  
    //your logic for whenever event is fired.
});

</script>

2
  • Hi @Nora - how are you instantiating the cometd such that $.cometd is valid ? I get an '$.cometd is undefined' error.. – CloudHugger Feb 9 '18 at 0:22
  • sorted - wasnt loading libraries correctly. – CloudHugger Feb 9 '18 at 0:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.