Last active
July 23, 2017 10:25
-
-
Save joshbirk/4534844 to your computer and use it in GitHub Desktop.
A Streaming API / jQuery example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest(seeAllData=true) | |
private class Test_MerchandisePushTopic { | |
// Tests go here | |
private static Integer numPushTopics() { | |
return [SELECT COUNT() FROM PushTopic WHERE Name = 'Merchandise']; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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}"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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