Sub topic 1
How publishers, the event bus, and subscribers fit together
Prerequisites
This chapter assumes you already know the following:
- Basic Apex syntax, including classes, triggers, and DML.
- Salesforce custom objects and fields.
- Basic Lightning Web Components, meaning JavaScript modules and lifecycle hooks.
- Apex governor limits and how a transaction works.
You do not need to have used platform events before. This chapter gives you the working model that the rest of the tutorial builds on.
The three roles
Platform events use an event-driven design. Instead of one piece of code calling another directly, a sender announces that something happened. Anything interested can react to the announcement.
- A publisher creates an event message and sends it. Publishers can be Apex code, flows, or an external system that calls the REST API.
- The event bus is the Salesforce service that receives events and holds them for a limited time. It sits between publishers and subscribers.
- A subscriber listens for events and reacts. Subscribers can be Apex triggers, platform event-triggered flows, Lightning Web Components, or external clients.
The publisher does not know who the subscribers are. A subscriber does not know who published. Both sides only agree on the event definition, which lists the fields in the message.
What an event is
A platform event is a message with fields that you define. It looks like a custom object, and the API name ends in __e, for example Order_Shipped__e. The similarity stops there. An event is not stored like a record. You cannot update or delete it, and you cannot query it with SOQL in the usual way. Once published, it exists on the event bus, and the bus keeps it for a retention window so that subscribers can replay it.
Why this design helps
Decoupling is the main benefit. You can add a second subscriber without changing the publisher. If a team later needs a notification when an order ships, they subscribe to Order_Shipped__e. The code that publishes the event stays as it is.
The design also separates transactions. A subscriber trigger runs in its own transaction, apart from the publisher's. Two consequences follow:
- Subscriber work does not count against the publisher's governor limits.
- A failure in a subscriber does not roll back the publisher's work.
Delivery is asynchronous
Publishing and delivery are two steps. The publisher gets a result that says whether the bus accepted the event. Delivery to subscribers happens afterward, on the bus's schedule. Do not write code that expects a subscriber to have finished by the time EventBus.publish returns.
Common mistakes
- Treating an event like a record. Events are not queryable and not editable, so do not plan reports or updates around them.
- Assuming order of completion. The publisher and subscribers do not finish in a fixed order.
- Tight coupling through event content. Put the facts about what happened in the event. Avoid fields that tell a specific subscriber what to do.
Exercise
A team publishes an OrderShipped_e event from Apex. A month later, a second team wants to send a customer email when orders ship. What should the second team do, and why does the publisher not need to change?
Show answer
The second team adds its own subscriber, such as an Apex trigger or a platform event-triggered flow, on OrderShipped_e. The publisher only sends events to the event bus and does not know who subscribes. The two sides share only the event definition, so adding a subscriber needs no change to the publisher.
Exercise
Name two effects of a subscriber trigger running in its own transaction, separate from the publisher's.
Show answer
Subscriber work does not count against the publisher's governor limits, and a failure in the subscriber does not roll back the publisher's work.