Define a Platform Event

Note

Note

As of the Spring ‘19 release (API version 45.0), you can build Lightning components using two programming models: the Lightning Web Components model and the original Aura Components model. Lightning web components and Aura components can coexist and interoperate on a page. This content covers Aura components. For more information about Lightning web components, see Introducing Lightning Web Components.

What You’ll Do

In this step, you define a custom platform event that conveys instant notifications.

You will:

  • Define a custom platform event.

  • Add a field to the platform event.

Introduction

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.

As part of this project, you:

  • 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 component.

  • Test your work by publishing a platform event via a provided Heroku app.

  • Implement a Chatter Apex trigger that publishes a platform event.

In the instant notification app that you build, events are published from a Heroku application (via the REST API) or from a trigger on Chatter posts (via Apex). Events are received by a Lightning console app that acts as a subscriber (via the lightning:empApi component that uses a CometD client connection). The following architecture diagram illustrates this process.

Architecture diagram of the project

Follow Along with Trail Together

Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live. You can find a link to the full session in the Resources section. 

Overview of Platform Events

Platform events enable developers to deliver secure, scalable, and customizable event notifications within the Salesforce platform or from external sources.

Platform events are based on a publish-subscribe architecture.

Apps can publish platform events by using Apex or one of the Salesforce platform APIs (SOAP, REST, or Bulk API). In addition, declarative tools such as the Lightning Process Builder or Cloud Flow Designer can publish platform events.

Apps can subscribe to events in various ways: through Apex triggers, your own CometD client, and the lightning:empApi component that uses a shared CometD client connection. CometD is a scalable HTTP-based event-routing bus. The use of CometD enables external apps to subscribe to platform events without the need for polling. In addition, apps can subscribe through point-and-click tools such as flows and processes. This module focuses on CometD-based subscription.

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 24 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.

My Domain Is Already On in Your Trailhead Playground

Do not attempt to turn on My Domain, or change its settings, in your Trailhead Playground. By default, My Domain is already active in every Trailhead Playground.

My Domain name highlighted in a Trailhead Playground URL

In your production org, My Domain lets you create a subdomain unique to your organization. With My Domain, you replace the instance URL that Salesforce assigns you, such as https://na17.lightning.force.com, with your chosen subdomain, for example, https://mydomainname.lightning.force.com.

My Domain is required to create custom Lightning components and set up single sign-on (SSO) in an org. To learn more about My Domain, check out this knowledge article. To learn how to activate it in your production org, see the User Authentication module.

Define a Platform Event

  1. 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

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities