Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Define a Platform Event

In this project, you'll:

  • Get an overview of platform events.
  • Set up your development environment.
  • Define a custom platform event.
  • Create a Lightning console app that displays instant notifications.
  • Subscribe to a platform event via the console app and the lightning/empApi module.
  • Test your work by publishing a platform event via a provided Heroku app.
  • Implement a Chatter Apex trigger that publishes a platform event.

Before You Begin

This project is designed for Salesforce developers who have some experience building Lightning web components. If you're not already familiar with lightning web components, we recommend that you complete some of the badges in the Build Lightning Web Components trail before continuing this project.

We assume that you have your Salesforce DX development environment set up, and you’re comfortable using it to create Lightning web components and deploy them to an org. If you’re not familiar with this process yet, complete the Quick Start: Lightning Web Components project before you continue in this project.

Introduction to the Bear Watch Instant Notification App

In this project you learn how to use platform events by building an instant notification app. With this application, park rangers can receive real-time notifications via the Salesforce platform.

Screenshot showing home screen with an instant notification alert saying that bears have been sighted near the visitor center.

In the instant notification app that you build, events are published from a Heroku application via the REST API or from an Apex trigger on Chatter posts. Events are received by a Lightning console app that acts as a subscriber to the Event Bus via the lightning/empApi module. The following diagram illustrates this architecture.

Architecture diagram of the project.

Overview of Platform Events

Platform events enable developers to deliver secure, scalable, and customizable event notifications. Events can be exchanged within the Salesforce platform or with external sources thanks to a publish-subscribe architecture. Apps can publish platform events by using Apex or one of the Salesforce platform APIs such as Pub/Sub, SOAP, REST or Bulk APIs. Flow can also publish platform events. Apps can subscribe to events in various ways: through Apex triggers, a Pub/Sub API (gRPC) client, a CometD client, the lightning/empApi module, or a Flow. CometD and gRPC are both HTTP-based event-routing protocols that enable external apps to subscribe to platform events without the need for polling. CometD is based on HTTP/1.1, whereas gRPC is based on the more modern HTTP/2 standard.

Note

Polling

Polling is the act of repeatedly querying a server for a resource. This technique is used to obtain a pseudo real-time response to the change of state of that resource. It is best not to use polling in a web page, because the practice does not scale.

Platform events contain customizable data just like custom objects do. Unlike custom objects, however, events persist for only 72 hours. And unlike records, there is no user interface that lists them, and you can't query them through SOQL.

For a more in-depth exploration of platform events, refer to the Platform Events Basics module and the Platform Events Developer Guide.

Create a New Trailhead Playground

For this project, you need to create a new Trailhead Playground. Scroll to the bottom of this page, click the playground name, then click Create a Trailhead Playground. It typically takes 3-4 minutes to create a new Trailhead Playground.

Note

Note

Yes, we really mean a brand-new Trailhead playground! If you use an existing org or playground, you can run into problems completing the challenges.

Get Your Trailhead Playground Username and Password

Let's get started. Go to your Trailhead Playground. (If it’s not already open, scroll to the bottom of this page and click Launch.) If you see a tab in your org labeled Get Your Login Credentials, great! Skip ahead to step 1. Otherwise, from the App Launcher (App Launcher), find and open Playground Starter and follow the steps.

  1. Click the Get Your Login Credentials tab and take note of your username.
  2. Click Reset My Password. This sends an email to the address associated with your username.
  3. Click the link in the email.
  4. Enter a new password, confirm it, and click Change Password.

Set Up Your Project

  1. In Visual Studio Code, create a Salesforce DX project named Instant Notification App.
  2. Authorize your Trailhead Playground.

Define a Platform Event

  1. Back in your Playground, from Setup, enter Platform Events in the Quick Find box, then select Platform Events.
  2. On the Platform Events page, click New Platform Event.
  3. Create a Notification platform event with the following settings:
    • Label: Notification
    • Plural Label: Notifications
    • Object Name: Notification
    • Description: Real time notifications
  4. Click Save.

Add a Field to the Platform Event

  1. On the Notification platform event page, click New in Custom Fields & Relationships.
  2. Select Text as the field data type.
  3. Click Next.
  4. Set the following values:
    • Field Label: Message
    • Length: 255
    • Field Name: Message
    • Description: The notification message
    • Required: Check the box next to "Always require a value in this field in order to save a record"
  5. Click Save.

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback