Streaming API Reference Links:
Https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming
Https://resources.docs.salesforce.com/210/latest/en-us/sfdc/pdf/api_streaming.pdf
Background: At work we may have such a requirement: Some data is important, real-time monitoring is needed for changes, or some data is integrated in other platforms. If there is a change, do not refresh the page or do other trigger and other operations can receive the relevant changes to the push notification (whether it is the Salesforce platform or other external services, such as the Java side, etc., in this way can be synchronized in real-time), tell the current need to monitor the record has changed. This scenario is often more likely to occur when the data is stored not only on the Salesforce side, but also on other platforms where data is stored or accessed. In this case, you can use the streaming API.
A. Stream API Brief Introduction
The streaming API provides two features, one for notifying clients of subscriptions based on Salesforce data changes, and one that is not based on the changes in Salesforce data to notify the client of a subscription. Next, most of the content is based on the changes to the Salesforce data, pushing the subscriptions to the client, and the other interested can see for themselves. Notification pushes can be applied not only to Salesforce systems, but also to other systems, such as OAuth, and can receive notification pushes after being connected, and interested in seeing what the API provides.
The streaming API is used to develop a set of conditions for the data you want to receive, and to specify which events (Create/update/delete/undelete) are eligible for the data to push a notification to the client, and a notification is the message that the event-triggered result is sent to the channel. The format of the notification is in JSON format. Use the streaming API to reduce unnecessary API requests.
The push technology used by the streaming API, that is, the server side will actively send notification messages to the subscribing client, rather than the client to call the server-side return message, using the Bayeux protocol and COMETD for long polling.
COMETD Reference Link: https://docs.cometd.org/
If you want to use the streaming API, you need the permissions of the Enable API and the permissions of the streaming API
To accept the notification (notifications), the user who is currently logged on must have Read permission for the Streamingchannel table
To create and manage notifications, the currently logged on user must have Create permission on Streamingchannel
Two. Streaming API usage steps
For developers, it's more about how this thing is used. Using the streaming API to enable subscribers to receive eligible push messages can take three steps:
1. Create Pushtopic
Remove the pushtopic magical veil, simply put, Pushtopic is a standard sobject that encapsulates the following fields:
- Apiversion: Used to specify the SQL version of the query, after 37 the system can store events within 24 hours, required fields;
- Description:pushtopic description information, limited to 400 characters or less;
- ID: Specifies the globally unique identity of a record;
- IsActive: is available, related to the Pushtopic limit count (System to pushtopic has limitation size);
- IsDeleted: Specifies whether this pushtopic is moved to the Recycle Bin;
- Name:pushtopic name, defines the name of the channel, and this name must be unique, late subscribers subscribe, the use of this name;
- Notifyforfields: Specifies which fields are evaluated to generate notifications;
- Notifyforoperations: Specifies which event action generates a notification after API version 29, which is a read-only field;
- Notifyforoperationcreate:create whether the operation will generate a notification, api29 later available;
- Notifyforoperationdelete:delete whether the operation will generate a notification, api29 later available;
- Notifyforoperationundelete:undelete whether the operation will generate a notification, api29 later available;
- Notifyforoperationupdate:update whether the operation will generate a notification, api29 later available;
- The QUERY:SOQL statement determines what data conforms to the triggered event and is sent to the channel.
Well, create a new pushtopic, set the value of the required field after insert, a pushtopic is created. Several of these fields need to be described in detail:
Query: The role of query in Pushtopic is self-evident, defining what data can be pushed to meet the criteria. Query syntax is basically the same as SOQL, but there are some cases that are not supported. The restrictions are as follows:
- The Select field has no ID;
- Child connection Query semi-joins and anti-joins;
- Aggregate queries such as SUM,AVG, etc.;
- COUNT;
- LIMIT;
- Relational queries are not supported and can only be queried to the ID, not through the "." Other information in the way that the parent is queried;
- The text area field is included in the query;
- ORDER by;
- GROUP by;
- The where section uses the formula Type field;
- Not example:select Id from account WHERE not Name = ' salesforce.com '
- OFFSET
- TYPEOF
Notifyforfields: The push message contains information about the value of a Sobject field, which field values will be pushed depending on the assignment of Notifyforfields, Notifyforfields Assignment and function as follows:
- All: Push all the fields
- Referenced (default): Pushes select and the collection of the set of the Where section fields, if this field value is not specified, the default value is referenced
- Select: Pushes only the fields of the Select section;
- where; push only the fields in the Where section.
notifyforoperationcreate: Specifies that the value of this field is true when the sobject of the operation is an insert operation and satisfies the query criteria of the queries, the server side pushes a notification to all subscribers, This field is allowed if and only if Apiversion is greater than 29;
Notifyforoperationdelete: Specifies that the value of this field is true when the sobject of the operation is a delete operation and satisfies the query criteria of the queries, the server side pushes a notification to all subscribers, This field is allowed if and only if Apiversion is greater than 29;
Notifyforoperationundelete: Specifies that this field value is true when the sobject of the operation is undelete and satisfies the query criteria The server side pushes a notification to all subscribers, which is allowed only if apiversion is greater than 29;
notifyforoperationupdate: Specifies that the value of this field is true when the operation's sobject is in the update operation and satisfies query criteria, the server side pushes a notification to all subscribers, This field is allowed if and only if Apiversion is greater than 29.
apiversion: This field declares the streaming version. In the 36.0 and before, he did not include the status of the client and could not track past event information. Simply put, if the declared version is 36 and in the past, after the client subscribes to the channel, it can only receive data that occurs after the subscription satisfies the condition of the data event, and the data that subscribes to the previous related event cannot be received. If this field is set to a value of 37.0 and above, the streaming API supports storing data notification information that satisfies the criteria within 24 hours, even after the client subscribes to the channel, and can replay previous data for 24 hours. A record may appear additions and deletions and many other events, each broadcast event by Replayid as the number, Replayid in the org and channel is unique, even if the event is deleted, this event corresponding to the Replayid will not be reused, Subscribers can replay by assigning a value Replayid to receive different event messages, with three Replayid assigned
-2: After the Subscriber connects to the channel, receives all events, including the first 24 hours of the subscription event message;
-1: After the Subscriber connects the channel, receives the post-subscription event message;
Replayid: After subscribers connect to the channel, they accept the specified Replayid and subsequent event messages, such as Replayid to 5, and Subscribers will receive 5 event messages.
(Note: Only one sobject can be searched in each query statement.)
When new Pushtopic wants to see if the creation is successful and whether it takes effect, or simulates Pushtopic's subscription, you can use Workbench to view the relevant status, and here, for example, listen for account additions and deletions, and the above event will send a notification.
1) Login workbench, URL https://workbench.developerforce.com/login.php, select the login version is 36.0
2) jump to select streaming Push Topics
3) Push Topics Select the testaccountstreaming that you just created, click Subscribe, and you have currently subscribed to this pushtopic
4) If an account information is modified, a specific change message will be displayed in streaming push Topic.
Pushtopic can be easily created by following the field description above, and the next step should be to have the client subscribe. How to make a subscription, at this point, we need to define a channel to ensure that the client can successfully subscribe to this channel, when the data that satisfies the condition event is triggered, the channel server is actively pushed to the client subscribing to this channel. The channel is also encapsulated as an object Streamingchannel on the Salesforce side,
The Streamingchannel table structure is as follows:
- Description of the Description:streaming channel
- ID of the Id:streaming channel
- IsDeleted: Specifies whether the streaming channel is moved to the Recycle Bin
- IsDynamic: If True, the channel is created dynamically at the time of subscription
- Lastreferenceddate: Stores the timestamp of the record last viewed by the current user
- Name: Pushtopic of the binding, starting with/u/, named/u/pushtopic name
- Owner of the Ownerid:streaming channel
One of the main fields to be described here is that the Name,name field must be/u/pushtopicname, and pushtopicname can only contain standard characters and ' _ ' or '/', other characters are not supported, For example, the name of the pushtopic created above is Testaccountpushtopic, and when Streamingchannel is created, name must be/u/testaccountpushtopic and case sensitive.
2. Client Subscription Channel
The client subscription channel can be easily divided into three parts:
- Send a handshake request
- Send a request for a subscription to a channel
- Connect using a long response (polling).
If the subscription is used in a page or component, it can be manipulated using COMETD, COMETD provides the basic method for implementing client subscription operations, as described in cometd API. When the channel is successful, the action events (Create/update/delete/undelete) that are performed by the data of the query defined by Pushtopic meet the operating conditions defined by the Pushtopic, and the channel will send a notification to all subscribed clients. It says that the notification is a JSON string, and the approximate format is as follows:
{ "clientId": "lxdl9o32njygi1gj47kgfaga4k", "data": { "event": { "createddate": "2016-09-16t19 : 45:27.454z ", " Replayid ": 1, " type ":" Created " }, " Sobject ": { " Phone ":" (415) 555-1212 ", "Id": "001d000000kneakiab", "Name": "Blackbeard" } , "channel": "/topic/accountupdates"}
Subscriber subscriptions receive messages that require the following permissions:
FLs for fields in the Where Condition section
Read permission for the query object
Read permission for the Pushtopic object
Permissions for New and edited records based on sharing rule
Salesforce stores 24 hours based on Pushtopic time and allows you to retrieve already stored and new events. Subscribers can choose which events to receive by replaying the option.
When you subscribe to a channel, you do not want to receive all the data, such as customer information, different people are more concerned about their customers change information, this is the URL you can add filter after the subscription to the event notification filtering, push the notification information you need.
eg:/topic/channelname?<expression>
where ChannelName the name of the channel defined above,<expression> contains one or more conditions, multiple conditions using ' & ' segmentation, such as
/topic/testaccountpushtopic?country=china&isactive=true
If you use the ID as the filter, you need to use the 18-bit ID format, and 15-bit is not supported.
Three. Code examples
The official offers two demos, one for the data-based DML operation sent to the subscriber message, and one for the active broadcast sent to the subscriber message.
Https://github.com/developerforce/SalesforceDurableStreamingDemo/archive/master.zip
Can be downloaded to look at the code, the code contains a detailed explanation of the client handshake, subscription and the operation of receiving information.
Summary: This article is mainly about the use of the streaming API and under what circumstances will use this API, if you use to view the above document.
Salesforce 0 Basic Learning (85) Streaming API Simple to use (near real-time get the updated message status of the data you need to track)