Plateform Event Basics
Before we jumped to understand the platform event, it is important to understand what are component or artifacts of an event-driven architecture. With the advent of the platform events, we really have the ability to implement the event-driven architecture. So that we can integrate different systems.
Event-driven Architecture:
It is like a typical publisher-subscriber model or we can say like the producer-consumer model. With the advent of the platform events, event-driven architecture is now possible. With the event-driven architecture the focus shifts from the entities involved in the integration to the actual message exchange that is exchanged between two entities. This architecture is much more loosely coupled so if we want to add or remove multiple producers or multiple consumers. It is very easy to do so. We can have different salesforce app or external app as a producer or the consumer.
Event Producer: A system produces or publishes events.
Event Consumer: it is the one that subscribes or listen to the events, takes in the event and processes the event
Event Bus: it is a centralized communication channel between producer and consumer.
Platform Event:
Platform Event is a sobject like a salesforce entity that you can create within your salesforce instance. Platform Event is suffixed with __e. We can define events based on the message exchange it means we can define different types of events for different types of integration. We can define a custom field and attributes to define the message. An event is nothing but the definition of the message that you be exchanging between the publisher and the subscriber. The apex setSavepoint() and rollback() Database methods aren’t supported with platform events. There is a standard field, which you get when you create a platform event and that is known as ReplayId. ReplayId is very important because that ReplayId can be used to replay that specific event.
Steps to Create Platform Events:
- From Setup, Search Platform Events and Click on the Platform Events.
- Click on the New button to create Platform Events.
- Provide the name for the Platform Events.
- Add custom fields to your custom event. Platform event custom fields support only these field types.
- Checkbox
- Date
- Date/Time
- Number
- Text
- Text Area (Long)
Publishing/Subscribing Platform Events:
We can publish platform events in 3 ways:
- Publish Events Messaging using APEX.
- Publish Events Messaging using Declarative tools (Process Builder or flow).
- Publish Events Messaging using Salesforce API from an external app.
Subscribing Platform Event:
- We can subscribe to platform events using Apex triggers. Apex triggers receive event notifications.
Write an “after insert” Apex trigger on the event object to subscribe to incoming events.
Triggers receive event notifications from various sources—whether they are published through Apex or APIs.
- We can Subscribe Platform Events using Declarative tools like Process Builder or flow.
- In an external app, you can subscribe to events using CometD. Visualforce and Lightning component apps receive events through CometD.
CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet.