Sunday, May 13, 2018

Salesforce Streaming API


Introduction
It’s a procedure which observe data changes in Salesforce based on the SOQL query result and sends notifications to all the subscriber. These notifications, called as Push Notifications, can be received from a VF page, a server and Java client etc.
Streaming API uses the Bayeux protocol and the CometD messaging library. This is used for providing an asynchronous message by HTTP. Also, known as Long polling.


Setup Streaming API

1. Define PushTopic
Create a record for the “PushTopic” object, which is a standard object. A PushTopic enables you to define the object, fields, and criteria you’re interested in receiving event notifications for. These Push topic records also known as Channel. There is no OOB screen to create these record, it has to be created via code. Below is the code to create a PushTopic for custom Product object.



2. Connect to the PushTopic channel (Subscribe)
Connect to the PushTopic channel by utilizing JavaScript or Java library which can be downloaded from http://cometd.org/. At a time, you can subscribe to several PushTopic channels from one client.

Step By Step Guide For Java Client
https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_java_client_intro.htm

GitHub Resource
  1. https://github.com/forcedotcom/EMP-Connector/
  2. https://github.com/developerforce/SalesforceDurableStreamingDemo
  3. https://github.com/developerforce/StreamingReplayClientExtensions

Sample Response received at subscriber client


3. Sending Notification from the Server
Condition evaluation will take place every time the record of the object PushTopic query is referring to is created/updated or deleted.


Message Durability
As of API version 37.0, Salesforce stores events that match PushTopic queries, even if no one is subscribed to the PushTopic. The events are stored for 24 hours, and you can retrieve them at any time during that window.
Each event notification contains a field called replayId. The replay ID is unique for the org and channel. When you replay an event, you’re retrieving a stored event from a location in the stored stream. You can either retrieve a stream of events starting after the event specified by a replay ID, or you can retrieve all stored events.























Considerations & Limitations
  1. The SELECT statement’s field list must include Id.
  2. There is only one entity that can be referred for one query. A relation query is not supported.
  3. Aggregation query, COUNT, LIMIT, ORDER BY, GROUP BY, OFFSET and TYPEOF cannot be used. Also, a formula field can be specified in a SELECT phrase but not in a WHERE phrase.
  4. Long Text area and Rich text area field can’t be specified in Select statement though changes in such fields could still be tracked by setting pushTopic.NotifyForFields to “All”.  It means Long Text area and Rich text area field can’t be a part of Payload.
  5. For delete operation, salesforce streaming API doesn’t sends the full data of record in payload, it just sends Salesforce record Id. This makes the delete operation difficult at subscriber side if the external schema doesn’t stores the salesforce Id (which is less likely).


Streaming API Allocations
These default allocations are for basic consumers of Streaming API.

If your application exceeds these allocations, or you have scenarios for which you need to increase the number of concurrent clients, contact Salesforce.



References:
  1. https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming
  2. https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/limits.htm
  3. https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/using_streaming_api.htm















No comments:

Post a Comment