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.
There 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.
- 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:
If you’ve had any interesting experiences or projects where you’ve had to use the Streaming API let us know in the comments!