Hi friends, In this tutorial, we are going to learn about Streaming API in Salesforce. The Streaming API in salesforce enables developers to send relevant information to users in real-time, instead of having to refresh the screen to get new information. It uses push technology to send data from the server to the client.

After completing this tutorial you will be able to understand:

  • What is Streaming API in Salesforce?
  • How to Streaming API works?
  • Example of using Streaming API in Salesforce.

What is Streaming API in Salesforce?

The Streaming API in salesforce enables developers to send relevant information to users in real-time, instead of having to refresh the screen to get new information. The Streaming API uses publisher/subscriber model to send data from server to client. In Streaming API Publisher/ Subscriber is a model in which data transfer is initiated by the server (Server) and to the client (Subscriber). The Salesforce Streaming API is used to broadcast custom notifications with platform events and generic steaming.

How does it work?

While working with Streaming API in Salesforce the first thing that the developer must do is set up a PushTopic in Salesforce. Push Topic is an object that contains the criteria of events you want to listen to, such as data changes for a particular object. You define the criteria as a SOQL query in the PushTopic and specify the record operations to notify on (create, update, delete, and undelete). Next, the client can subscribe to that Push Topic and receive data whenever a change is made to the specified field against the action such as Create, Update, Delete, UnDelete.
Streaming API uses the Bayeux protocol and CometD, so the client to server connection is maintained through long polling.

  • Bayeux is a protocol used for broadcasting asynchronous messages primarily over HTTP.
  • CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet. It implements the Bayeux protocol.

To receives information from the server the client must maintain a connection to the server

Example of using Streaming API:

Suppose I want a notification when an Opportunity record is created or updated. But before going ahead we need to make sure a couple of settings for Streaming API at Org and Profile level.

1- Goto Setup and search for User Interface and Build> Customize. Make sure that the Streaming API checkbox is checked as shown in below image.

Streaming API

2- On Profile under Administrative Privileges make sure that the API enabled checkbox is checked.


Step 1- Create Push Topic:

Now the first topic that we need to take is to create Push Topic. We need to execute the following code in Anonymous window of Developer Console.

String sStageName='Closed Lost';
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'OpportunityCreateUpdate';
pushTopic.Query = 'SELECT Id, Name FROM Opportunity';
pushTopic.ApiVersion = 36.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.otifyForOperationUpdate = true;
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;

Step 2: Downloadn CometD:

=> Download the CometD compressed archive (.tgz) file from here.

I=> In the extracted folder you will find the war file at location “cometd-2.2.0/cometd-javascript/common/target”.

=> Unpack that war file using below DOS command in Windows and same command will work on shell also

cd cometd-2.2.0/cometd-javascript/common/target
jar xvf cometd-javascript-common-2.2.0.war org/cometd.js

If you will not follow the above step you wull get the following error.

Uncaught ReferenceError: org is not defined

Step 3- Extract the following JavaScript files from “cometd-2.2.0-distribution.tar.gz”

cometd-2.2.0/cometd-javascript/common/target/org/cometd.js (This file must be from war file)

Trouble in finding libraries? You can Directly download from here

Step 4: Upload Static Resource.

Upload the above the JS files in Static Resource with the following names.

  • cometd.
  • jquery2
  • json2
  • jquerycometd

Step 5- Create an Apex Controller.

Create an Apex controller called StreamingAPIController . Replace the following following code with below code

public class StreamingAPIController {
public StreamingAPIController() {
public List<Opportunity> getlistOpportunity() {
return [SELECT Id, Name,StageName FROM Opportunity ];
public PageReference refresh() {
return null;

Step 6- Create Visualforce Page.

Create a visualforce page called pushnotificationtest and write the following code.

<apex:page controller="StreamingAPIController">
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery2}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquerycometd}"/>
<script type="text/javascript">
$(document).ready(function() {
// Connect to the CometD endpoint
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/OpportunityCreateUpdate', function(message) {
function disconnect() {
window.onbeforeunload = disconnect;
<apex:form >
<apex:actionFunction name="callRefresh" action="{!refresh}"/>
<apex:pageBlock >
<apex:pageBlockTable value="{!listOpportunity}" var="objOpp">
<apex:column value="{!objOpp.Name}"/>
<apex:column value="{!objOpp.StageName}"/>

Save and Preview the visualforce page.

To Test the Streaming API functionality in new tab Create an Opportunity Record you will see as soon as the opportunity record is created the visualforce in the first tab will get automatically refreshed.

View More:


I hope you would enjoy this interesting topic. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are always appreciated.

Thank You,