The Force.com Streaming API lets you expose a near real-time stream of data from the Force.com platform. Administrators can create topics, to which applications can subscribe, receiving asynchronous notifications of changes to data in Force.com, via the Bayeux Protocol. Streaming data made simple, secure and scalable.
Before Digging Deep into the Logic First Download the Following Zip Files and Upload them in Static Resources
There are 3 steps in creating Real Time Notifications Using Streaming API
1) Upload files in static Resources
2) Build a query of fields you want to access and create your pushtopic.
3) Write Your logic for handling the notifications received.
1) Upload files in static Resources : -
Download Below rar file, extract and Upload each file. Remember What you are naming while uploading as you need to access this files in your vf page.
Click To Download
2) Build a query of fields you want to access and create your pushtopic : -
Visual Force page Code :-
Hope this Helps..
Before Digging Deep into the Logic First Download the Following Zip Files and Upload them in Static Resources
There are 3 steps in creating Real Time Notifications Using Streaming API
1) Upload files in static Resources
2) Build a query of fields you want to access and create your pushtopic.
3) Write Your logic for handling the notifications received.
1) Upload files in static Resources : -
Download Below rar file, extract and Upload each file. Remember What you are naming while uploading as you need to access this files in your vf page.
Click To Download
2) Build a query of fields you want to access and create your pushtopic : -
Query
The first step in using the Streaming API is to define a SOQL query that will return the records you are interested in. For example, your application may require a notification whenever a new Account is created. A suitable query might be:
1 | SELECT Id, Name FROM Account |
Alternatively, you might only be interested in Accounts with more than 1,000 employees:
1 | SELECT Id, Name FROM Account WHERE SomeField> 1000 |
As a special case, you can specify a record id in the
WHERE
clause and receive notifications whenever any change occurs on that record:1 | SELECT Id, Name FROM Account WHERE Id = '001x0000002JSofAAG' |
These are just some simple examples - see the Streaming API documentation for a full discussion of Streaming API queries.
PushTopic
Once you have decided on your query, it's time to create a
PushTopic
that binds a topic name to the query. You can set the following fields on the PushTopic
record:Field Name | Type | Example | Description |
---|---|---|---|
ApiVersion | double | 23.0 | Required. API version to use for executing the query specified in Query. |
Description | string | All records for the Account object. | Optional. Description of what kinds of records are returned by the query. |
Name | string | NewAccounts | Required. Descriptive name of the PushTopic. The maximum length is 25 characters. |
Query | String | SELECT Id, Name FROM Account | Required. The SOQL query statement that determines which record changes trigger events to be sent to the channel. Maximum length is 400 characters. |
There are also several system fields on
PushTopic
such as CreatedById
and SystemModstamp
.
As soon as you create a
PushTopic
record, applications can subscribe to the topic.
Go ahead and create a topic now. Login to your Developer Edition environment, Go to Developer console and navigate to Execute Anonymous Block and copy paste below code.
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'new account'; // you can specify name of your choice
pushTopic.Query = 'SELECT Id, Name FROM account';
pushTopic.ApiVersion = 31.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationUndelete = true;
pushTopic.NotifyForOperationDelete = true;
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;
3) Write Your logic for handling the notifications received: -Visual Force page Code :-
<apex:page showHeader="false" standardController="Account" extensions="streamingcontroller" id="page" >
<!-- the static resource files uploaded-->
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>
<apex:includeScript value="/support/console/25.0/integration.js"/>
<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/newaccount', function(message)
{
//You can use message as it will return you many attributes
//I am just using to track that event is generated
RefreshedAccounts();
});
});
})(jQuery)
</script>
<apex:form id="form" style="padding:0px;margin:0px;">
<apex:actionFunction name="RefreshedAccounts" action="{!newaccountlist}" reRender="block">
</apex:actionFunction>
<apex:pageBlock mode="maindetail" id="block">
<apex:pageblockTable value="{!myaccounts}" var="a">
<apex:column width="50%" >
<apex:outputField value="{!a.name}" id="name" />
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller Code :- public class streamingcontroller{
public streamingcontroller(ApexPages.StandardController controller) {
storinglist = new list<account>();
}
//variables declaration
public list<account> storinglist{set;get;}
//get method of list
public list<account> getmyaccounts(){
storinglist= [select id,name from account order by id desc];
return storinglist;
}
public void newaccountlist(){
getmyaccounts();
}
}
Hope this Helps..