A simple way to experiment with the Streaming API is to create a Visualforce page to serve as the subscriber. You can then visually see notifications as they arrive. Figure 11.1 shows a sample Visualforce page to do this. The button on the top starts and stops notifications by creating and deleting a PushTopic record. The table below it displays notifications as they arrive from Force.com, in response to the creation and modification of Timecard records.
To try this example in your own Salesforce organization, create the controller class in Listing 11.1. Then download the CometD library at http://download.cometd.org/cometd-2.2.0-distribution.tar.gz. Uncompress it and extract the following files:
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
Place them into a zip file and upload it as a static resource named cometd
. Now you can create the Visualforce page given in Listing 11.2.
public with sharing class MyPageController11_1 {
public Boolean started { get; set; }
private static final String TOPIC_NAME = 'TimecardUpdates';
public MyPageController11_1() {
started = 1 == [ SELECT count() FROM PushTopic
WHERE Name = :TOPIC_NAME ];
}
public PageReference stop() {
PushTopic p = [ SELECT Id from PushTopic
WHERE Name = :TOPIC_NAME LIMIT 1];
if (p != null) {
delete p;
}
started = false;
return null;
}
public PageReference start() {
PushTopic p = new PushTopic();
p.Name = TOPIC_NAME;
p.Query = 'SELECT Id, Name, Status__c FROM Timecard__c';
p.ApiVersion = 28.0;
p.NotifyForOperations = 'All';
p.NotifyForFields = 'Referenced';
insert p;
started = true;
return null;
}
}
<apex:page controller="MyPageController11_1">
<apex:form id="form">
<apex:includeScript value="{!URLFOR($Resource.cometd,
'Cometd.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.cometd,
'jquery-1.5.1.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.cometd,
'jquery.cometd.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.cometd,
'json2.js')}"/>
<apex:sectionHeader title="Force.com Streaming API Example" />
<br />
<apex:commandButton action="{!start}" value="Start"
rerender="form" rendered="{!NOT started}" />
<apex:commandButton action="{!stop}" value="Stop"
rendered="{!started}" />
<apex:outputPanel id="comet" rendered="{!started}">
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$.cometd.init({
url: window.location.protocol + '//' + window.location.hostname +
'/cometd/28.0/',
requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
});
$.cometd.subscribe('/topic/TimecardUpdates', function(message) {
$('#content').append(
'<tr><td>' + JSON.stringify(message.channel) + '</td>' +
'<td>' + JSON.stringify(message.data.sobject.Name) + '</td>' +
'<td>' + JSON.stringify(message.data.sobject.Id) + '</td>' +
'<td>' + JSON.stringify(message.data.event.type) + '</td>' +
'<td>' + JSON.stringify(message.data.event.createdDate) + '</td>' +
'</tr>'),
});
});
})(jQuery)
</script>
</apex:outputPanel>
<p />
<table id="content" width="80%"><tr><th>Channel</th><th>Name</th>
<th>Id</th><th>Type</th><th>Created</th></tr>
</table>
</apex:form>
</apex:page>
3.145.174.253