Three Force.com Streaming API Techniques

Streaming API is a great tool for data integration that requires notifications triggered by data changes in your organization. But sometimes, no matter how great a technology is, there may be scenarios that it doesn’t directly address. In this blog post, I’ll talk about some of these scenarios and propose techniques that you can use to address them with Streaming API.

Set the Order of Event Notifications

Changes to data in your organization happen in a sequential manner. But in Streaming API, the order in which you receive event notification messages isn’t guaranteed. So a frequent question that developers ask is: how can I handle the ordering of messages received by my client when using Streaming API?

As you can see from the notification below, each notification has event information including type and createdDate.

  • The type indicates what type of event triggered the notification (create or update of a record).
  • The createdDate indicates what time the event occurred.
{
  "channel": "/topic/InvoiceStatementUpdates",
  "data": {
  "event": {
  "type": "updated",
  "createdDate": "2012-04-24T22:45:58.000+0000"
  },
    "sobject": {
    "Name": "INV-0000",
    "Id": "a00D0000008oLpOIAU",
    "Description__c": "This is a test.",
    "Status__c": "Pending"
    }
  }
}

One way you can handle ordering of notifications is to use the createdDate in your client to order the messages returned in a channel. The createdDate is a UTC date/time value, so if your client doesn’t support that, you can convert it with the appropriate function in your client programming language.

Trigger a Notification when a Record is Deleted

Another question that gets asked frequently is how to use Streaming API to get notifications when a record is deleted. Currently, you can configure it so that Streaming API generates a notification when a record is updated or created. But there’s no way to push a notification to the channel when a record is deleted. You could work around this by creating an object that logs record deletes. Then your PushTopic query would reference that object so that a notification is created when a delete log record is created.

Trigger a Notification when a Long Text Field is Updated

Right now, you can’t use Streaming API to generate event notifications when a long text field changes. If you attempt to reference a long text field in the SOQL query that’s the basis of a PushTopic, you’ll receive an INVALID_FIELD error.

One way you can trigger channel notifications from updates to a long text field would be to create another Boolean field in your object and an Apex trigger. When the long text field data is changed, the trigger fires and updates the Boolean field. You can then reference the Boolean field in the PushTopic query. After you receive a notification, your client will need to explicitly retrieve the long text field data using the ID that’s returned in the notification.

For more information, see the documentation and this wiki page. If you come up with any other creative ways to implement Streaming API, let me know!

Published
May 8, 2012
Topics:

Leave your comments...

Three Force.com Streaming API Techniques