Streaming API is useful when you want notifications to be pushed from the server to the client based on criteria that you define.
The sequence of events when using Streaming API is as follows: (explained in detail later)
1. Create a PushTopic based on a SOQL query. This defines the channel.
2. Clients subscribe to the channel.
3. A record is created or updated (an event occurs). The changes to that record are evaluated.
4. If the record changes match the criteria of the PushTopic query, a notification is generated by the server and received by the subscribed clients.
Before we get started, I recommend you to go through these topics:
1) Push Technology
2) Bayeux Protocol, CometD, and Long Polling
We will achieve this once we are done with all steps (given below):
Now follow these steps:
Prerequisites:
1) The “Streaming API” permission must be enabled -> "Your Name > Setup > Customize > User Interface"
2) The logged-in user must have “Read” permission on the PushTopic standard object to receive notifications.
3) The logged-in user must have “Create” permission on the PushTopic standard object to create and manage PushTopic records.
4) The logged-in user must have “Author Apex” permissions to create a PushTopic by using the Developer Console.
Step 1: Create an Object - In our case we will use Account
Step 2: Create a PushTopic
Use the Developer Console to create the PushTopic record. You can use either Developer Console or Workbench. If needed these records can be updated.
"Your Name > Developer Console"
PushTopic pushTopic = new PushTopic(); pushTopic.Name = 'RefreshAccounts'; pushTopic.Query = 'SELECT Id, Name FROM Account'; pushTopic.ApiVersion = 26.0; pushTopic.NotifyForOperations = 'All'; pushTopic.NotifyForFields = 'Referenced'; insert pushTopic;
Step 3: Create Static resource
1. Download the CometD compressed archive (.tgz) file from http://download.cometd.org/cometd-2.2.0-distribution.tar.gz
2. 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
File Name
|
Static Resource Name
|
cometd.js
|
Cometd
|
jquery-1.5.1.js
|
Jquery
|
json2.js
|
json2
|
jquery.cometd.js
|
jquery_cometd
|
Step 4: Test the PushTopic Channel
Here is the code sample
Visualforce Page
<apex:page id="PG" controller="StreamingAPIController"> <apex:form id="FRM"> <apex:includeScript value="{!$Resource.cometd}"/> <apex:includeScript value="{!$Resource.jquery}"/> <apex:includeScript value="{!$Resource.json2}"/> <apex:includeScript value="{!$Resource.jquery_cometd}"/> <apex:actionFunction name="GetRefreshedAccounts" reRender="PB,PBT"/> <script type="text/javascript"> (function($) { $(document).ready(function() { // Connect to the CometD endpoint $.cometd.init({ 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 // In this example we are using this only to track the generated event $.cometd.subscribe('/topic/RefreshAccounts', function(message) { //You can use message as it will return you many attributes //I am just using to track that event is generated GetRefreshedAccounts(); }); }); })(jQuery) </script> <apex:pageBlock id="PB"> <apex:variable var="count" value="{!0}" /> <apex:pageBlockTable id="PBT" value="{!getRefreshedAccount}" var="AllAcc"> <apex:column headerValue="S.No."> <apex:variable var="count" value="{!count+1}" /> {!count} </apex:column> <apex:column value="{!AllAcc.Name}" headerValue="Name"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
Apex Class
public class StreamingAPIController { //Everytime page is reRendered it will get refreshed values of account public List<Account> getRefreshedAccount { get { return [select Id, Name from Account LIMIT 50000] ; } set; } public StreamingAPIController() { } }
This is very small and simple sample code, you can do a lot more with streaming APIs. Please refer these documents to learn more:
1) http://www.salesforce.com/us/developer/docs/api_streaming/api_streaming.pdf
2) http://www.salesforce.com/us/developer/docs/api_streaming/index.htm
3)http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_Streaming_API