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

11 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
  3. A person does not need to wait very long in order to view their streaming video, since all the computer needs to do is connect to the host server and buffer the video. moviebox app

    ReplyDelete
  4. iptv stands for Internet Protocol Television. This concept is getting popular in the here and now. The features of IPTV offer a great viewing experience and allow you to view your desired content.

    ReplyDelete
  5. The information you have posted is very useful. The sites you have referred was good. Thanks for sharing.. streaming

    ReplyDelete
  6. streaming complet Avec streamavie.com vous pouvez regarder en complet et gratuitement les meilleurs films du box office en streaming VF, VO ou VOSTFR sans inscription.

    ReplyDelete
  7. streaming vf Avec streamavie.com vous pouvez regarder en complet et gratuitement les meilleurs films du box office en streaming VF, VO ou VOSTFR sans inscription.

    ReplyDelete
  8. It's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act. Voir film

    ReplyDelete
  9. thank you for choosing out the length to talk about this, I ecosystem all-powerful about it and love analyzing more upon this topic. it is agreed beneficial for me. thanks for this sort of critical gain in the back of again. https://totalsportek.news/f1-streaming

    ReplyDelete

Powered by Blogger.