Wednesday, 7 August 2024

STREAMING API - Subscription Mechanism

Salesforce Streaming API allows you to stream real-time events and data to your applications.

 It's beneficial for scenarios where you need to receive notifications or updates as they occur, 

such as changes in Salesforce records, custom events, or platform events


Here's an overview of the key concepts, setup, and usage of the Salesforce Streaming API:

******Generic Events also possible through salesforce streaming api


  • PushTopic events (legacy) - less preferable and old technology
  • Generic events (legacy) - less preferable and old technology

                



====================================================================

PushTopics

Advantages:

  • Selective Notifications: You can specify SOQL queries to listen for changes to specific records that match your criteria. This can be more efficient if you only care about a subset of records.
  • Familiarity: If you are already familiar with SOQL, creating PushTopics can be straightforward.

Disadvantages:

  • Limited Fields: PushTopics only notify you of changes to a specific set of fields that you specify in your SOQL query.
  • Notification Types: You need to specify which operations (create, update, delete, undelete) you want to be notified about. This might require multiple PushTopics for different operations.

=======================================================================

Change Data Capture (CDC)

Advantages:

  • Comprehensive Notifications: CDC provides notifications for all changes (create, update, delete, undelete) to records and includes all fields, not just a subset.
  • Simpler Management: No need to create multiple topics for different operations; one CDC stream can cover all changes.
  • Field-Level Data: Each event includes detailed information about the changes, including old and new values.

Disadvantages:

  • Volume of Data: CDC can produce a high volume of data since it includes all changes. This may require more bandwidth and processing power to handle.
  • Less Selective: If you only need to track specific changes, you might need to implement additional filtering logic on the client-side.


======================================================================

When to Use PushTopics

  • You need to monitor specific changes to a subset of records or fields.
  • You prefer to use SOQL queries to define your criteria.
  • You want to minimize the volume of data being sent to the client by only tracking relevant changes.

When to Use Change Data Capture (CDC)

  • You need comprehensive tracking of all changes to records, including all fields.
  • You want a simpler setup that does not require creating multiple topics for different operations.
  • You need detailed change information with both old and new values.
  • You need to keep external systems in sync with Salesforce with minimal delay.

=======================================================================

Platform Events

Use Cases:

  • Custom Event-Driven Architectures: When you need to create custom events that do not necessarily correlate directly with changes to standard or custom Salesforce objects. For example, you might want to publish an event whenever a user completes a specific action in a custom app.
  • Integration with External Systems: When you need to integrate with external systems that listen for and act upon events generated by Salesforce. Platform Events provide a flexible and scalable way to broadcast events across different systems.
  • Complex Event Handling: When you require custom event handling logic, such as chaining events or implementing complex workflows.

Advantages:

  • Custom Events: You can define custom events with any fields you need.
  • Flexible Triggering: Events can be published from Apex, Process Builder, or Flow Builder, offering flexibility in how events are triggered.
  • Replayability: Events can be replayed from a specified point in time, useful for error recovery and data synchronization.

Disadvantages:

  • Limited Retention: Events are retained for 72 hours, which means you need to handle events promptly.
  • Limited Size: Each event message has a maximum size of 1 MB.
  • Governor Limits: Platform Events are subject to Salesforce governor limits and usage limits, which may impact high-volume use cases.

=======================================================================


====================================================================


Generic Events in Salesforce are a part of the platform's event-driven architecture. They provide a lightweight and flexible way to create and manage notifications that are not tightly coupled to Salesforce records or objects. Generic Events are suitable for scenarios where you need to emit and handle events that don't necessarily correspond to changes in Salesforce data.

Overview of Generic Events

Generic Events are a subset of Salesforce’s event system, and they are used to publish and subscribe to events that are more abstract or high-level, not tied to specific Salesforce objects. They are typically used when you want to send notifications or trigger processes that are not directly related to a Salesforce record.

Key Characteristics

  1. Event Definition:

    • Unlike Platform Events and Change Data Capture (CDC), Generic Events do not require you to define a schema related to Salesforce objects. Instead, you can define the event schema according to your needs.
  2. Publishing Events:

    • You can publish Generic Events from Salesforce via Apex, Flow, or Process Builder. This allows you to trigger notifications or actions based on custom logic or business processes.
  3. Subscribing to Events:

    • External systems or Salesforce components can subscribe to Generic Events to receive notifications or take actions. This can be done using CometD in external applications or via Apex in Salesforce.
  4. Replayability:

    • Like Platform Events, Generic Events can also be replayed for a limited period (typically 72 hours), which is useful for handling missed events.

Use Cases for Generic Events

  1. Custom Notifications:

    • Use Generic Events to send notifications that are not tied to specific Salesforce data changes but are relevant to business processes, like a system status update or a user action.
  2. Integration:

    • Publish events from Salesforce to notify external systems about specific actions or statuses that are not directly related to record changes.
  3. Business Logic:

    • Trigger complex business logic or workflows based on custom events that are part of your application's broader event management system.

Implementation of Generic Events

1. Define a Generic Event

  • In Salesforce:
    • Go to Setup.
    • Search for Generic Events and create a new event definition.
    • Define the schema for the event, specifying the fields that will be part of the event.

2. Publish a Generic Event

  • Using Apex:

    apex
    // Define the Generic Event MyGenericEvent__e event = new MyGenericEvent__e(); event.Field1__c = 'Value1'; event.Field2__c = 'Value2'; // Publish the Event EventBus.publish(event);
  • Using Process Builder:

    • Create a new Process in Process Builder.
    • Define the criteria for when the event should be published.
    • Choose the action to Send Custom Notification or Publish Event and select the Generic Event.

3. Subscribe to a Generic Event

  • Using CometD (Java Example):
    java
    import org.cometd.client.BayeuxClient; import org.cometd.client.transport.LongPollingTransport; import org.cometd.client.transport.Transport; import org.cometd.websocket.client.WebSocketTransport; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.websocket.client.WebSocketClient; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; public class SalesforceGenericEventClient { private static final String SALESFORCE_INSTANCE_URL = "https://yourInstance.salesforce.com"; private static final String COMETD_ENDPOINT = SALESFORCE_INSTANCE_URL + "/cometd/56.0"; private static final String SESSION_ID = "your_salesforce_session_id"; private static final String GENERIC_EVENT_TOPIC = "/event/MyGenericEvent__e"; public static void main(String[] args) throws Exception { HttpClient httpClient = new HttpClient(); httpClient.start(); Map<String, Object> options = new HashMap<>(); WebSocketClient wsClient = new WebSocketClient(); wsClient.start(); Transport websocket = new WebSocketTransport(options, null, wsClient); Transport longPolling = new LongPollingTransport(options, httpClient); BayeuxClient client = new BayeuxClient(COMETD_ENDPOINT, websocket, longPolling); client.addExtension((ext, message) -> { if ("handshake".equals(message.getChannel())) { Map<String, Object> auth = new HashMap<>(); auth.put("ext", new HashMap<String, String>() {{ put("Authorization", "OAuth " + SESSION_ID); }}); message.putAll(auth); } return true; }); CountDownLatch latch = new CountDownLatch(1); client.handshake((message) -> { if (message.isSuccessful()) { System.out.println("Handshake successful"); // Subscribe to the Generic Event channel client.getChannel(GENERIC_EVENT_TOPIC).subscribe((channel, msg) -> { System.out.println("Received Generic Event message: " + msg); }, (subMsg) -> { if (subMsg.isSuccessful()) { System.out.println("Subscription successful"); } else { System.err.println("Subscription failed: " + subMsg); } }); latch.await(1, TimeUnit.MINUTES); } else { System.err.println("Handshake failed: " + message); } }); } }

4. Handling Generic Events

  • In Salesforce:
    • Use Apex triggers or Lightning components to handle Generic Events.
    • Implement custom logic based on the event data and business requirements.

Comparison with Other Event Types

FeaturePlatform EventsChange Data Capture (CDC)Generic Events
PurposeCustom event-driven architectures, IntegrationData synchronization, Audit and complianceCustom notifications, Integration
CustomizationCustom event schema with specific fieldsAutomatic tracking of Salesforce recordsCustom event schema with any fields
Event TypesCreate, Update, Delete, UndeleteCreate, Update, Delete, UndeleteCustom-defined
Retention72 hours72 hours72 hours
ReplayabilityYesYesYes
Use Case ExamplesUser actions, Integration with external systems, Custom workflowsData synchronization, Audit trail, Real-time updatesCustom notifications, Business process triggers

Conclusion

  • Use Platform Events for custom event handling and integration scenarios that require a specific event schema related to business processes.
  • Use Change Data Capture (CDC) for comprehensive tracking and synchronization of Salesforce record changes.
  • Use Generic Events for high-level notifications and integration scenarios where the events are not tied to specific Salesforce records but are relevant to broader business processes.

By understanding the strengths and limitations of each type of event, you can choose the most appropriate mechanism for your specific needs in Salesforce.

No comments:

Post a Comment

Heap Size and Apex CPU Time Limit

  Heap Size - 1) Memory size for holding object , variables and records 2) CPU Time limit -  1) time consuming during whole transaction   ...