Salesforce Streaming API: Real-Time Insights

Salesforce Streaming API: Real-Time Insights

Streaming API enables streaming of events using push technology and provides a subscription mechanism for receiving events in near real time. The Streaming API subscription mechanism supports multiple types of events, including PushTopic events, generic events, platform events, and Change Data Capture events.

Common Terms used in Streaming API:

• Event : Either the creation of a record or the update of a record. Each event may trigger a notification.
• Notification : A message in response to an event. The notification is sent to a channel to which one or more clients are subscribed.
• PushTopic : A record that you create. The essential element of a PushTopic is the SOQL query. The PushTopic defines a Streaming API channel.


Step 1 : Create PushTopic

To create PushTopic, we will need the developer console of the Salesforce and assume that the name of the topic is “WonOpportunity”.

Push Topic pushTopic = new PushTopic();
pushTopic.ApiVersion = 23.0;
pushTopic.Name = ‘Won Opportunity’;
pushTopic.Description = ‘Notify system on opportunity stage change changes to closed won’.
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'Referenced';
pushtopic.Query = 'Select o.OwnerId, o.Name, o.IsWon, o.Id, o.Amount From Opportunity o WHERE o.IsWon = true';
insert pushTopic;

Step 2 : Download and install required packages


Download the CometD compressed archive (.tgz) file and store in static resource. You can find the distribution at below link

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


Sample Visualforce Page

$jq(function() {

// Connect to the CometD endpoint
url: window.location.protocol+’//’+window.location.hostname+’/cometd/23.0/’,
requestHeaders: { Authorization: ‘OAuth {!$Api.Session_ID}’}
$jq.cometd.subscribe(‘/topic/WonOpportunity’, function(message) {

After saving this page, open same salesforce instance in other browser or tab and make changes to any opportunity with status “won”, it will display an alert that opportunity is won.
JSON Response:
following JSON response we get from Streaming API. The Rows returned in Object depends upon the Push topic create earlier.

Below is the Visualforce page example where such type of streaming can be used for keeping eye on real-time data.

Screenshot (46)