What is the Salesforce Streaming API?
The Salesforce Streaming API is a simple way to push relevant data to your users in real time, instead of having to refresh the screen to get new information.
This capability enables users to get information without having to perform any action to retrieve it - essentially pushing new data to their screens.
How does Salesforce Streaming API work?
The Streaming API uses a publisher-subscriber (pub/sub) model to push data to the client. The first thing a developer must do is set up a PushTopic in Salesforce. This is basically a query that watches for changes in values of certain fields in a Salesforce Object. Then the client can subscribe to that PushTopic and receive updates whenever a change is made to the specified fields. This is done using a push protocol called the Bayeux protocol. The Salesforce implementation uses long polling connections to the server that will be able to notify the client on changes. This differs from traditional polling in that it keeps an incoming request open until an event is fired and then sends the response.
Here’s how this works. An HTTP request is made to the server. Instead of responding immediately, the server keeps the request and waits for a specific event to fire, in our case a change in one of the fields of our PushTopic object. When the event is fired a response is made. Then the client makes another request to the server which it keeps open and waits. This ends up looking something like this:
Ok… but how can I use this?
Let’s say we’re the Orange Party Cup Company, and we've invented a revolutionary new party cup (one that is orange as opposed to red). We also sell and manufacture these cups. Our salespeople are on the phone day and night trying to get our plastic cups into the hands of party-goers everywhere, as quickly as possible.
We need to create a system that would notify our warehouse immediately every time a new order came in, so they can package, ship it, and mark it as completed in our Salesforce org.
Create an Order Object
So first we need to create our order object. This is where our salespeople will enter order information such as address and quantity. Not very complicated but necessary in order to create a PushTopic that watches for the creation of this object. Mine looks like this:
Create a PushTopic
Next, we must create our PushTopic. The easiest way to do this is to either use the workbench at developerforce.com or the developer console. I’m going to use the developer console. In the console head to Debug -> Execute Anonymous Apex Code. We are essentially creating a SOQL query with a few extra parameters that watch for changes in a specified object.
Note we have added a Name so that we can subscribe to this specific topic, and we only want notifications on the ‘Create’ operation. If you were to choose ‘All’ it would notify every time one is created, or an update is made to any of the fields in your query.
The easiest way to get the files you need is to unarchive the .war file located in
Then zip up the ‘org’ and ‘jquery’ folders, and call the new zip file streaming.zip. It should look like this:
Upload this new file as a static resource to your org. With the static resources in place, we can create our VF page and controller. Mine looks like this:
OrderStream Visualforce Page
That’s it! (pretty simple, right?) Now we can ship our revolutionary orange party cups faster than ever before. For more insights into Salesforce and other great content, check out the Appirio Resource Hub