Apex

Streaming API in Salesforce with Example

Streaming API in Salesforce with Example


Streaming API's are useful when you want to push the Notifications from Server to Client . It is best suited for applications which keep polling Salesforce for record Changes . These applications would be benefited using Streaming APIs as it will  reduce the unnecessary API calls and processing time of the requests which return No Data.

Before we start developing ,Check the below :
• The “Streaming API” permission must be enabled.
 Note: To verify that the “API Enabled” and “Streaming API” permissions are enabled in your organization, from Setup, enter User Interface in the Quick Find box, then select User Interface.

Step1: Create a Push Topic 

1. Open the Developer Console. 
2. Click Debug > Open Execute Anonymous Window. 
3. In the Enter Apex Code window, paste in the following Apex code, and click Execute.

PushTopic pushTopic=new PushTopic();
pushTopic.Name = 'AccountUpdates';
pushTopic.Query = 'SELECT Id, Name,type from Account';
pushTopic.ApiVersion = 36.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationUndelete = true;
pushTopic.NotifyForOperationDelete = true;
pushTopic.NotifyForFields = 'Referenced';

insert pushTopic;

Step2:    Download the Zip and extract the files: http://s000.tinyupload.com/index.php?file_id=80570159672625190356
From Setup, enter Static Resources in the Quick Find box, then select Static Resources to add the extracted files with the following names:

File Name                      Static Resource Name 
cometd.js                        cometd
jquery-1.5.1.js                jqueryS
json2.js                           json2
jquery.cometd.js             jquery_cometd

Step3: Create Visualforce Page : 

Copy and paste the below code  :
<apex:page >
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jqueryS}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>
<script type="text/javascript">
13
Example: Visualforce Page Step 2: Create a PushTopic
(function($){
$(document).ready(function() {
// Connect to the CometD endpoint
$.cometd.init({
url:
window.location.protocol+'//'+window.location.hostname+'/cometd/24.0/',
requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
});
// Subscribe to a topic. JSON-encoded update will be returned
// in the callback
$.cometd.subscribe('/topic/AccountUpdates', function(message) {
$('#content').append('<p>Notification: ' +
'Channel: ' + JSON.stringify(message.channel) + '<br>' +
'Record name: ' + JSON.stringify(message.data.sobject.Name) +
'<br>' + 'ID: ' + JSON.stringify(message.data.sobject.Id) +
'<br>' + 'Event type: ' + JSON.stringify(message.data.event.type)+
'<br>' + 'Created: ' + JSON.stringify(message.data.event.createdDate)
+
'</p>');
});
});
})(jQuery)
function disconnect() {
$.cometd.disconnect();
}
window.onbeforeunload = disconnect;
</script>
<body>
<div id="content">
<h1>Streaming API Test Page</h1>
<p>This is a demonstration page for Streaming API. Notifications from the
accountUpdates channel will appear here...</p>
</div>
</body>
</apex:page>


Step4: Open the newly created page. On a different tab create a new account. You will see the
notification popping up on the VF page. 

About Saurabh Dua

4 comments:

  1. Did not get any pop up on VF page. Followed the same steps,is anything I am missing. check boxes are also checked.

    ReplyDelete
    Replies
    1. Check if the static resources are loaded successfully.

      Delete
  2. Hello Saurabh,
    I tried but it is not working. followed all the steps accurately.

    PS; i am following almost all of your posts..thanks for the information you are sharing.

    ReplyDelete
    Replies
    1. HI Raghavendra,

      If you are still stuck , kindly go through the link given below. https://resources.docs.salesforce.com/sfdc/pdf/api_streaming.pdf

      Delete

Powered by Blogger.