Streaming API in Salesforce with Example | Techila
single,single-post,postid-1250,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

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 let us work with the settings:

       The “Streaming API” permission must be enabled.

       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.


Following are steps to do:

Step 1: 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;


Step 2: Download the Zip and extract the files:

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


Step 3: 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”>
Example: Visualforce Page Step 2: Create a PushTopic
$(document).ready(function() {
// Connect to the CometD endpoint
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( + ‘<br>’ +
‘Record name: ‘ + JSON.stringify( +
‘<br>’ + ‘ID: ‘ + JSON.stringify( +
‘<br>’ + ‘Event type: ‘ + JSON.stringify(
‘<br>’ + ‘Created: ‘ + JSON.stringify(
function disconnect() {
window.onbeforeunload = disconnect;
<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>

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

Thats all you can do!!!



Author: techila

No Comments

Post A Comment