Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Last active July 23, 2017 10:25
Show Gist options
  • Save joshbirk/4534844 to your computer and use it in GitHub Desktop.
Save joshbirk/4534844 to your computer and use it in GitHub Desktop.
A Streaming API / jQuery example
//clear old ones out
List<PushTopic> pts = [SELECT ID, Name From PushTopic WHERE Name = 'Merchandise'];
delete pts;
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'Merchandise';
pushTopic.Description = 'All records for the Account object';
pushtopic.Query = 'SELECT Id, Name, Quantity__c FROM Merchandise__c WHERE Quantity__c < 50';
pushTopic.ApiVersion = 26.0;
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;
System.debug('Created new PushTopic: '+ pushTopic.Id);
public class Util_MerchandisePushTopic {
public static void deleteTopics() {
List<PushTopic> pts =
[SELECT Id, Name FROM PushTopic WHERE Name = 'Merchandise'];
delete pts;
}
public static void createTopic() {
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'Merchandise';
pushTopic.Description = 'Merchandise items that are getting low on stock';
pushtopic.Query =
'SELECT Id, Name, Quantity__c FROM Merchandise__c WHERE Quantity__c < 50';
pushTopic.ApiVersion = 26.0;
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;
System.debug('Created new PushTopic: ' + pushTopic.Id);
}
}
@isTest(seeAllData=true)
private class Test_MerchandisePushTopic {
// Tests go here
private static Integer numPushTopics() {
return [SELECT COUNT() FROM PushTopic WHERE Name = 'Merchandise'];
}
}
static testmethod void testDeleteMerchandisePushTopics() {
Util_MerchandisePushTopic.deleteTopics();
Util_MerchandisePushTopic.createTopic();
System.assert(0 < numPushTopics());
Test.startTest();
Util_MerchandisePushTopic.deleteTopics();
Test.stopTest();
System.assert(0 == numPushTopics());
}
static testmethod void testCreateMerchandisePushTopics() {
Util_MerchandisePushTopic.deleteTopics();
Integer numTopicsBeforeCreate = numPushTopics();
System.assert(0 == numTopicsBeforeCreate);
Test.startTest();
Util_MerchandisePushTopic.createTopic();
Test.stopTest();
Integer numTopicsAfterCreate = numPushTopics();
System.assert(numTopicsAfterCreate == (numTopicsBeforeCreate + 1));
}
<apex:includeScript value="{!URLFOR($Resource.jQueryCometD, 'cometd.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQueryCometD, 'json2.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQueryCometD, 'jquery-1.5.1.js')}" />
<apex:includeScript value="{!URLFOR($Resource.jQueryCometD, 'jquery.cometd.js')}"/>
<apex:includeScript value="{!$Resource.ICanHaz}"/>
<script type="text/javascript">
j$ = jQuery.noConflict();
j$(document).ready(function() {
configuration = {url:'https://'+window.location.hostname+'/cometd/26.0/',
requestHeaders: {"Authorization": "OAuth {!$Api.Session_ID}"},
appendMessageTypeToURL : false};
j$.cometd.configure(configuration);
j$.cometd.handshake();
j$.cometd.subscribe('/topic/Merchandise', function(message) {
console.log(message);
alert(message.data.sobject.Name + ' has only '+message.data.sobject.Quantity__c+' units.');
});
});
</script>
<H1>Recent Warehouse Activity</H1>
<table id="warehouse">
<tr><td><strong>Merchandise Item</strong></td><td><strong>Quantity</strong></td></tr>
<script type="text/html" id="merch_tmpl">
<tr><td><a href="/{{Id}}" target="_new">{{Name}}</a></td><td>({{Quantity__c}})</td></tr>
</script>
</table>
j$.cometd.subscribe('/topic/Merchandise', function(message) {
console.log(message);
merch = ich.merch_tmpl(message.data.sobject);
j$('#warehouse').append(merch);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment