SALESFORCE STREAMING API: REAL-TIME INSIGHTS
Streaming API enables the 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 events, including PushTopic events, generic events, platform events, and Change Data Capture events.
What is the Salesforce Streaming API?
The Salesforce Streaming API allows developers and administrators to subscribe to data streams and receive real-time updates when specific events or changes occur in Salesforce. Unlike traditional API calls, which require polling to check for updates, the Streaming API pushes the updates directly to the client, ensuring low latency and up-to-the-minute information.
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.
Steps:
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 http://download.cometd.org/cometd-2.2.0-distribution.tar.gz
Extract the following JavaScript files from “cometd-2.2.0-distribution.tar.gz”
cometd-2.2.0/cometd-javascript/common/target/org/cometd.js
cometd-2.2.0/cometd-javascript/jquery/src/main/webapp/jquery/jquery-1.5.1.js
cometd-2.2.0/cometd-javascript/jquery/src/main/webapp/jquery/json2.js
cometd-2.2.0/cometd-javascript/jquery/src/main/webapp/jquery/jquery.cometd.js
Sample Visualforce Page
$jq(function() {
// Connect to the CometD endpoint
$jq.cometd.init({
url: window.location.protocol+’//’+window.location.hostname+’/cometd/23.0/’,
requestHeaders: { Authorization: ‘OAuth {!$Api.Session_ID}’}
});
$jq.cometd.subscribe(‘/topic/WonOpportunity’, function(message) {
createEle(message.data.sobject);
});
});
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.
![]()
Step 4: Test the Implementation
To test the setup, follow these steps:
- Open the Visualforce page in one tab or browser.
- In a different tab, update an opportunity to have a status of Won.
- A real-time alert will appear in the Visualforce page indicating the opportunity has been closed as won.
Conclusion
The Salesforce Streaming API provides an efficient way to monitor real-time events, like updates to important records, without the need for continuous polling. By creating PushTopics and using the CometD library, you can receive and display these updates in your applications, improving data visibility and responsiveness. This method is highly scalable and reduces the load on the API while ensuring you get critical data in real time.
Contact Us
We would love to hear from you Please feel free to send us a message via the form