Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Pub Sub with Force.com Streaming API

jeffdonthemic edited this page Dec 19, 2012 · 1 revision

Added the force.com streaming client inside CloudSpokes rails website with Restforce gem. Since CS rails website uses thin server which start EventMachine, the pub/sub streaming client can be implemented by EM.next_tick. Pull Request

http://poller-in-ruby.herokuapp.com/
Please use the submission description for the user account to try the demo app.

Create Objects

Create Member, Challenge and Mail objects. I used Member and Challenge object from cloudspoke packcage and created Mail__c as described in the challenge.

Create PushTopics on Salesforce

Create PushTopic on Member__c object

PushTopic pushTopic = new PushTopic();
pushTopic.ApiVersion = 26.0;
pushTopic.Name = 'AllMembers';
pushTopic.Description = 'All new or updated Member__c object';
pushtopic.Query = 'SELECT Id FROM Member__c';
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'All';
insert pushTopic;

Create a PushTopic on Challenge__c object

PushTopic pushTopic = new PushTopic();
pushTopic.ApiVersion = 26.0;
pushTopic.Name = 'AllChallenges';
pushTopic.Description = 'All new or updated Challenge__c object';
pushtopic.Query = 'SELECT Id FROM Challenge__c';
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'All';
insert pushTopic;

Create PushTopic on Mail__c object

PushTopic pushTopic = new PushTopic();
pushTopic.ApiVersion = 26.0;
pushTopic.Name = 'AllMails';
pushTopic.Description = 'All new or updated Mail__c object';
pushtopic.Query = 'SELECT Id FROM Mail__c';
pushTopic.NotifyForOperations = 'create';
pushTopic.NotifyForFields = 'All';
insert pushTopic;

Streaming Client with Restforce

Add the faye gem in the Gemfile.

gem 'faye'

The streaming client code is implemented in the initializer config/initializers/streaming.rb. Please provider your Salesforce credentials for Restforce in streaming.rb.

client = Restforce.new :username => 'your-username,
  :password       => 'your-password',
  :security_token => ‘your-security-token’,
  :client_id      => 'your-client-id',
  :client_secret  => your-client-secret

This code subscribes to the Salesforce PushTopic AllMembers and print member name.

client.subscribe 'AllMembers' do |message|
  id = message['sobject']['Id']
  members = client.query("select Name from Member__c where Id = '"+id+"' limit 1")
  member = members.first
  puts "Streamed member: #{member.Name}"
end

Testing

Please configure your Salesforce credentials in to config/initializers/streaming.rb.

Then run the CS website

rails s

Then... create or update a Member__c record
create or update a Challenge__c record
create Mail__c record

The streaming client will print the streamed object to the console!

Clone this wiki locally