Monday, 2 March 2015



What is Streaming API ?

To put it simple, streaming API is a mechanism which monitors data changes in Salesforce based on the SOQL query you define, and if the condition matches, sends push notifications to all the subscribed clients.

Since Summer ’12, streaming API is available in any API-valid organizations. You can test it to see how it works in the Developer Edition.

Also, push notifications can be received from a page in a Salesforce application, a server and a page outside of Salesforce, and Java client.

For this example, we will use a Visualforce page to receive push notifications.

What is a Push Notification ?

Push notification is the technology that allows you to send information without the clients’ request when an event occurs in the server.

In "pull technology" where information is sent in response to the clients’ request, the page will not be updated unless the user intentionally refreshes or changes the page. Therefore, in order for the client to know an event has occurred in the server, you need to send a page request periodically and receive the result (called polling).

On the server side, a series of processes to generate an HTTP connection, send information, and close the connection by each and every polling is necessary. iI polling is performed by many clients, then it would consume a huge amount of server resources and network bandwidth.

If you use Ajax that sends a request asynchronously by restricting one part of the page, you can reduce some network bandwidth consumption. However, this is not effective as you still need to respond to the requests that are sent regularly regardless of the server event.

I would not recommend you to use polling frequently, especially if you are using Salesforce, because polling from outside of Salesforce, such as SOAP/REST API consumes API request counts.

This is when streaming API becomes useful.

Note : Before going to code we need some java script files 

STEP 1 :  Go through the URL  and download the zip file  (

STEP 2 :  Extract the zip file and follow the below process 

              Next, upload the following files as static resources (Your Name > Setup > Develop > Static 

              Resources > New)

                FILE                                                 NAME

             cometd.js                                             cometd
             jquery-1.5.1.js                                      jquery

             json2.js                                                 json2

             jquery.cometd.js                                  jquery_cometd

STEP 3 :  we have to create one push topic on object 
            Go to the developer console or workbench copy the below code and executive it 

            PushTopic pushTopic = new PushTopic();
       pushTopic.ApiVersion = 29.0;
       pushTopic.Name = 'pushOnAccout';
       pushTopic.Description = 'All records for the Account object';
       pushtopic.Query = 'SELECT Id, Name,Phone FROM Account';
       insert pushTopic;
       System.debug('Created new PushTopic: '+ pushTopic.Id);


                Visual force Page :

<apex:page controller="StreamingApiController"> 
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>

<script type="text/javascript">

    $(document).ready(function() {

        url: window.location.protocol+'//'+window.location.hostname+'/cometd/29.0/',
        requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
    $.cometd.subscribe('/topic/pushOnAccout', function(message) {
        var list = new Array();



    $('#content').append('<p>Notification: ' +
        'Channel: ' + JSON.stringify( + '<br></br>' +
        'Record name: ' + JSON.stringify( + '<br></br>' + 
        'ID: ' + JSON.stringify( + '<br></br>' +
        'Event type: ' + JSON.stringify('<br></br>' +
       'Created: ' + JSON.stringify( + '</p>');


<div id="content">
<p>Notifications should appear here...</p>

           Controller : 

            public class StreamingApiController {
                public static void methodTosendMessage(List<String> streamData){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(new string[] {''});
                    mail.setCcAddresses(new string[] {''});
                    mail.setSubject('Record ' + streamData[3]);
                    mail.setPlainTextBody('Record Details ' + streamData);
                    Messaging.SendEmailResult[] res = Messaging.sendEmail(new             
                    Messaging.SingleEmailMessage[] { mail });       
Post a Comment