1

I have created a demo of streaming api. I have followed all the steps which have mentioned in the dec for streaming api as same http://www.salesforce.com/developer/docs/api_streaming/. The only thing is that I named my PushTopic as AccountUpdates and used my account object instead of invoice custom object all also updated the name of PushTopic in my Vf page also and activated the PushTopic from user interface.

My code

PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'AccountUpdates';
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;

My Visualforce page

<apex:page sidebar="false">
    <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">
    (function($){
            $(document).ready(function() {
                    // Connect to the CometD endpoint
                    $.cometd.init({
                         alert('ready');
                         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
                 $.cometd.subscribe('/topic/AccountUpdates', function(message) {
                         $('#content').append('<p>Notification: ' +
                                    'Channel: ' + JSON.stringify(message.channel) + '<br>' +
                                    'Record name: ' + JSON.stringify(message.data.sobject.Name) +
                                    '<br>' + 'ID: ' + JSON.stringify(message.data.sobject.Id) + 
                                    '<br>' + 'Event type: ' + JSON.stringify(message.data.event.type)+
                                    '<br>' + 'Created: ' + JSON.stringify(message.data.event.createdDate) + 
                                    '</p>');
                    });
            });
    })(jQuery)
    </script>

    <body>
      <div id="content">
        <h1>Streaming API Test Page</h1>
        <p>This is a demonstration page for Streaming API. Notifications from the 
         InvoiceStatementUpdates channel will appear here...</p>    
      </div>
    </body>
</apex:page>

But I am not able to see any updates in my Visualforce page if I update it or create records. But when I try to use workbench it works and display update over there. Please do help me out getting confused.

1

A few suggestions:

Make sure there's a div on your page with id="content"

Throw a js console.log(message) before the append functions, in case the API is working fine but your js isn't.

Finally, I always use $j=jQuery.noConflict(); and then $j instead of $ when using jQuery on visualforce.

| improve this answer | |
  • Thanks for your suggestion. I not to gud with jQuery can you please let me know which line of code I need to replace with $j=jQuery.noConflict(); – Anu Sep 7 '14 at 3:25
  • 1
    Anu, insert the line right at the start of your javascript, before (function($){ and then use j$ in place of $ throughout the rest of your code. Salesforce already has a version of jQuery bound to $ and that can intefere with your own code. – Matt Lacey Sep 10 '14 at 0:57
0

I had the same issue the first time I went through the Streaming API tutorial.

** Double and triple check that you loaded your static resources right... specifically checking that the right FILE has been labeled with the right SFDC resource NAME that your code is calling **

http://www.salesforce.com/us/developer/docs/api_streaming/index_Left.htm#StartTopic=Content/code_sample_java_add_source.htm

The Files

cometd-2.2.0/cometd-javascript/common/target/cometd-javascript-common-2.2.0.war
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/ Static Resource Name

cometd.js/ cometd
jquery-1.5.1.js/ jquery
json2.js/ json2
jquery.cometd.js/ jquery_cometd

*** Don't just upload the .war as your "cometd", you have to go in there and pull out the right .js file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.