The Force.com Streaming API


The Streaming API isn’t new but I thought I’d put together a post to document my experiences and get some source code onto Github to help you get started.

Streaming APIThere are different ways to interact with the Streaming API i.e. from within the Force.com Platform using Visualforce, or from outside the platform using the language of your choice. Either way you’ll need to implement the Bayeux protocol, which is easier than it sounds because there are many libraries out there to help you out.

At it’s core all you need to do is create a push topic – which is the registration of an SOQL query for use with the Streaming API – that the client subscribes to. The client then waits for updates or inserts and handles the data appropriately. I’m not going to go into too much detail since a lot is covered in the docs, but if you’re interested in a working example you can get my source code here. Note that for my source code to work you’ll need to create a push topic more or less in the following way:

PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'AllAccounts';
pushTopic.Query = 'SELECT id, name FROM Account';
pushTopic.ApiVersion = 26.0;
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;

You’ll need to execute this code using the System Log or the Execute Anonymous capabilities of any appropriate app.

Lessons Learned

  • The API will only feed through information that is inserted/updated after the page/app implementing the API opened/started.
  • You cannot use aggregate queries e.g. you could not create a running total of the sales amount this month.
  • You cannot use it with Chatter objects.
  • You can only have 10 concurrent subscribers i.e. only 10 instances of your page/app could subscribe to a topic.
  • You can use Workbench to experiment with the Streaming API as shown below:

Workbench and the Streaming API

If you’ve had any interesting experiences or projects where you’ve had to use the Streaming API let us know in the comments!

Tagged , ,

3 thoughts on “The Force.com Streaming API

  1. mattreyuk says:

    I’ve been playing with this recently and a few things I found are:
    1) If you use cometd javascript, you really need to use version 2.2 – later versions don’t seem to work (be careful which cometd.js file you grab too)
    2) you have to include the id field in your topic query
    3) you can’t use relationships in your query (eg owner.name) – use a formula field or trigger to create this data if you need it.

  2. Mike says:

    I think # concurrent clients is 20 now. However, to me this is definitely a showstopper. I mean why would I use it, if I can only get 20 users using it at a time? I know it says you can contact salesforce to increase these, but all customers who install your app have to get these increased? Most orgs would have more than 20 users I would think.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: