Wednesday, 9 June 2021

The Four Streaming APIs in Salesforce

 1.Generic events

2.Push Topic events

3.Platform Events

4.Change Data Capture events


These four Streaming APIs are part of what we call the Enterprise Messaging Platform (EMP).


Generic and PushTopic came as the "first generation", then Platform Events and Change Data Capture came second generation. 

While the first generation of APIs is quite mature now, it is not evolving as much as the second one.


A Shared Model :



All Streaming APIs are built on the same core technology:CometD.

This technology relies on a messaging bus that offers a publish/subscribe mechanism for events.


The publish/subscribe model follows the following scenario:


1.Event consumers connect to the messaging bus and subscribe to specific channels (event types)

2.Event producers connect to the bus and publish an event on a channel

3.The bus broadcasts the event to all registered subscribers


This means that the message producer is fully decoupled from the consumers. This model allows to build event-driven architectures at scale.

Typical use cases for this are system integrations and real-time apps.


1.Generic 


Data Structure    : Unstructured (String)

Publication       : REST API

Subscription      : code 


2.PushTopic  


Data Structure    : Defined by target sObject retrieved via a SOQL query

Publication       : Automated

Subscription      : code


3.Platform Events 


Data Structure    : Custom typed fields

Publication       : Delarative or Code

Subscription      : Delarative or Code


4.Change Data Capture 


Data Structure    : Defined by target sObject

Publication       : Automated

Subscription      : Delarative or Code


Streaming API provides two types of events that you can publish and subscribe to : PushTopic and generic.


PushTopic events track field changes in Salesforce records and are tied to Salesforce records.

Generic events contains arbitrary payloads.


Streaming API :


Streaming APIs enables transmitting of real data from Salesforce Platform.

Transmission of data done using push technology.

Push technology transfers information that is initiated from a server to the client.

Provides a subscription mechanism for receiving events in near real time.

Push technology also called the publish/subscribe model.


Pull Technology :


1.Client request data from the server periodically.

2.Clients poll the server for data updates, and freshness of data depends on the poll frequency.

3.Clients can make excessive calls and cause server slowdown.


Push Technology :


1.Notifications are sent to the subscriber in real time.

2.No pooling required.


Mechanism of Streaming API :


To enable Publisher/Subscriber model, active connection is required between Salesforce and each client.


Short Polling :


Client keeps sending to make active connection.


Long Polling :


1.Clients send requests for information but with the expectation the server may not respond immediately.

2.Server respond when data is changed for object.

3.Long polling supported using Bayeux Protocol or CometD.


Push Topic :


PushTopic events provide a secure and scalable way to receive notifications for 

changes to Salesforce data that match a SOQL query you define.


1.Receive notifications of Salesforce record changes ,including create,update delete and undelete.

2.Capture changes for the fields and records that match a SOQL query.

3.Recieve change notifications for only the records a user has access to based on sharing rules.


ex :


PushTopic pushTopic = new PushTopic();  

pushTopic.Name = 'AccountPush';  

pushTopic.Query = 'SELECT Id, Name, Type, Phone, Website  FROM Account WHERE Active=\'Yes\'';  

pushTopic.ApiVersion = 48.0; 

pushTopic.NotifyForOperationCreate=true; 

pushTopic.NotifyForOperationUpdate=true;

pushTopic.NotifyForOperationUndelete=true;

pushTopic.NotifyForOperationDelete=true;

pushTopic.NotifyForFields='Referenced';

insert pushTopic; 


Note :

NotifyForFields is used to determine when ontification should be sent.

NotifyForFields in PushTopic values.


All,Referenced(default),Select,Where


1.All - considers changes in all fields.

2.Referenced - consider changes in fields in both SELECT and WHERE clauses.

3.SELECT - considers changes in fields in SELECT clause.

4.WHERE - considers chnages in fields in WHERE clause.


PushTopic Security :


1.To receive notification user must have access to object.

2.Field Level Access required.

3.Only record where user has access will be returned.


Considerations - Multiple Notifications in Same Transaction :


1.Streaming API notifications sent in Reverse Order Within a Trasaction:


In general, event notifications are delivered in the order of record changes.


When a record triggers multiple notifications within the same transaction, the last 

notifications are delivered first.


2.Multiple Streaming API Notifications for the Same Record and Untracked Fields :


If field change triggers other changes on the same record, more notifications are sent for those fields.


3.Only Last PushTopic Notification Sent for the Same Record


When multiple PushTopic notifications are generated for the same record within 

about one millisecond and in the same transaction,only the last notification is sent.


LIMITS :


Maximum number of topics (PushTopic records) per org

UE: 100 EE: 50 All other editions: 40


Maximum number of concurrent clients (subscribers) across all channels and for all event types

UE: 2000 EE: 1000 All other editions: 20


Maximum number of delivered event notifications within a 24-hour period, shared by all CometD clients

UE: 1000000 EE: 200000 All other editions: 50000 (10000 for free orgs)


Socket timeout during connection (CometD session)

UE : 110 seconds EE: 110 seconds All other editions: 110 seconds


Timeout to reconnect after successful connection (keepalive)

UE : 40 seconds EE : 40 seconds All other editions: 40 seconds


Maximum length of the SOQL query in the Query field of a PushTopic record

UE : 1300 characters EE : 1300 characters All other editions: 1300 characters


Maximum length for a PushTopic Name

UE: 25 characters EE: 25 characters All other editions: 25 characters



You can use the following queries to review the usage

SELECT count() FROM PushTopic

SELECT count() FROM PushTopic WHERE IsActive = true



Generic Events :


Use generic events to send custom notifications that are not tied to Salesforce data changes.


1.Publish and receive arbitrary payloads in JSON without a predefined event schema.

2.Broadcast notifications to a target set of users,specific teams,or your entire org.

3.Send notifications for events that are external to salesforce.


How to use Generic Events ?


1.Streaming Channel


A StreamingChannel that defines the channel, with a name that is case-sensitive.

It is object and can be accessed from app menus


2.Clients


one or more clients subscribed to the channel.


3.Streaming Channel Push REST API


Streaming Channel Push REST API to monitor and invoke push events on the channel.

No comments:

Post a Comment