Ways to Subscribe to a Platform Event in Salesforce

|
| By Sonali Sharma

Platform Events in Salesforce allow systems to communicate using an event-driven approach.

When a Platform Event is published, different subscribers can receive and process it almost in real time. The way you subscribe depends on your use case (UI update, backend logic, external system, etc.).

This document explains the different ways to subscribe to a Platform Event and when to use each one.

What is a Platform Event?

A Platform Event is a special object in Salesforce used to send messages between systems.

It works like this:

Publisher → Sends event
Subscriber → Receives event

Key Features

  • Runs asynchronously (in the background)
  • Uses a Publish/Subscribe model
  • Stores events for a limited time (usually 24–72 hours)
  • Supports Replay ID to reprocess missed events

Methods to Subscribe to a Platform Event

You can subscribe using:

  1. Apex Trigger
  2. Lightning Web Component (EMP API)
  3. CometD (External systems)
  4. Pub/Sub API
  5. MuleSoft
  6. Flow (No-code option)
1. Apex Trigger (Inside Salesforce)
An Apex Trigger runs automatically when a new Platform Event is received.
 When to Use :
  • Business logic processing
  • Creating or updating records
  • Validation logic
  • Internal system automation
Example:

trigger OrderEventTrigger on Order_Event__e (after insert) {

    for (Order_Event__e eventRecord : Trigger.New) {

        System.debug(‘Event Received: ‘ + eventRecord.Order_Number__c);

    }

}

Key Points:
  • Runs in the background (asynchronous)
  • Supports bulk records
  • Can perform DML (insert/update/delete)
  • Cannot stop the event from being published
Best For – Backend automation inside Salesforce
2. Lightning Web Component (EMP API)
Used when you want the UI to react immediately when an event happens.
When to Use
  • Real-time notifications
  • Live dashboard updates
  • Auto-refresh screens
How It Works

User Screen

Subscribe using EMP API

Event received

UI updates automatically

Example

import { subscribe } from ‘lightning/empApi’;

const channelName = ‘/event/Order_Event__e’;

subscribe(channelName, -1, message => {

    console.log(‘New event received: ‘, message);

});

Best For – Real-time UI updates.
3. CometD (External Applications)
External systems can connect to Salesforce using the Streaming API (CometD protocol) to receive events.
Requirements
  • OAuth authentication
  • Access token
Channel Format

/event/EventName__e

Flow

External App

Authenticate

Connect to Salesforce

Subscribe

Receive events

Best For – Custom applications (Java, Node.js, Python, etc.)
4. Pub/Sub API (Recommended for New Integrations)
A modern API provided by Salesforce for high-performance event streaming.
Why Use It
  • Faster than CometD
  • Supports replay
  • Handles high volumes
  • Better for enterprise integrations
Best For – Large-scale and enterprise-level integrations.
5. MuleSoft Subscription 
MuleSoft can subscribe to Salesforce Platform Events using the Salesforce Connector.
Setup Steps
  1. Add Salesforce Connector
  2. Configure OAuth
  3. Use “On New Platform Event” listener
  4. Add processing logic
Example Flow

On New Platform Event

Logger

Transform Message

Call API / Save to DB / Send to Queue

Best For –Middleware and system-to-system integrations.
6. Flow (No-Code Option)
What It Is
You can subscribe to Platform Events using Flow without writing code.
Steps:
  1. Create a Platform Event–Triggered Flow
  2. Select the Event
  3. Add conditions
  4. Add actions (Create/Update record, Send Email, etc.)
  5. Activate
Best For – Simple automation managed by admins.
Replay ID & Event Recovery

Every event has a Replay ID.

It helps you recover missed events.

Replay Options
  • -1 → Only new events
  • -2 → All stored event
  • Specific Replay ID→ Start from that event
Why It Is Important
  • System restart recovery
  • Error handling
  • Guaranteed message processing

Leave a Reply

Your email address will not be published. Required fields are marked *