Platform Events

  • Platform Events Architecture
  • Event Object
  • Demo - Publishing events via API
  • Demo - Subscribing to events via Process Builder
  • Demo - Subscribing to events using CometD in Lightning Component

Platform Events Architecture

Platform Architecture Architecture

Platform Events Components

  • Provides an event-driven messaging architecture to enable apps to communicate inside and outside of Salesforce
  • Consists of event producers, event consumers, and channel (Event Bus)
  • Publishers and subscribers communicate with each other through events
  • One or more subscribers can listen to the same event and carry out actions

Quiz - 1

  • Channel and Event Bus are the same thing?

Sliding Window

Platform Events

Sliding window is 24 hours (now, may change)

Event Object Types

  1. Standard Events
    • AssetTokenEvent (Represents an event associated with an asset token, such as token issuance and registration of a connected device as an Asset)
    • OrgLifecycleNotification
  2. Change Events
  3. Custom Events
    • Entities with suffix: __e

Sample Event Object

Notification with custom field: Message

Event object - special kind of entity

  • It is different from Standard and Custom objects.
  • An event message is an instance of a platform event, similar to how a record is an instance of a standard/custom object
  • Update or delete event records not allowed
  • Can’t view event records in the Salesforce UI and don’t have page layouts
  • Deleting a platform event definition will result in permanent deletion
    • Event definition and all events in the topic are deleted
  • You can grant permissions to users in profiles or in permission sets

Event object - special kind of entity - contd.

  • Events are in their own transaction. So if you publish from apex, it cannot be rolled back, but the publish is a transaction
  • The allOrNoneHeader API header is ignored when publishing platform events through the API.
  • The Apex setSavepoint() and rollback() Database methods aren’t supported with platform events.
  • DML limits and other Apex governor limits apply for publishing platform events

Quiz - 2

  • Can we view event records in the Salesforce UI ?

Custom field types supported

  • Checkbox
  • Date
  • Date/Time
  • Number
  • Text
  • Text Area (Long)

Quiz - 3

  • Field type Checkbox is supported for Event Object?

ReplayId System Field

  • Identifies a platform event record
  • Used to get stored events that are within the retention window
  • The subscribed CometD client can retrieve past events using the ReplayId field
  • The ReplayId is unique for the org and the channel (Event Bus)
  • Similar to Kafka's offset
  • Notes about Apache Kafka

Publishing Event Message with Apex

           // Call method to publish events (pubEvents)
           List results = EventBus.publish(pubEvents);
       

Publishing Event Message with API

POST to the Endpoint: /services/data/v41.0/sobjects/Event_Name__e/
In our case, Event_Name__e is Notification__e

Sample request payload:

            { "Message__c" : "Power Off" }
          

Publishing Event Message with API - Demo

Publish  Event  with API Event payload

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder - contd.

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder - contd.

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder - contd.

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder - contd.

Note we are using Email Alert as the action for the event received

Subscribe to Event Message with Process Builder

Subscribe to Event Message with Process Builder - contd.

Subscribe to Event Message with Process Builder

Email Template Setup

Email Template Setup

Email Alert Setup

Email Alert Setup

Email Alert - result of Event Subscription Action

Email Alert

Subscribe Event Message with cometD

Channel Name: /event/Event_Name__e/

cometD Endpoint: /cometd/41.0

Sample response payload:

            { "data": {
                  "schema": "dffQ2QLzDNHqwB8_sHMxdA",
                  "payload": {
                    "CreatedDate": "2017-26-11T18:31:40Z",
                    "CreatedById": "005D0000001cSZs",
                    "Message__c": "Power Off"
                  },
                  "event": { "replayId": 2 }
                },
                "channel": "/event/Notification__e"
              }
          

Subscribe Event Message with cometD - Example

Subscribe Event Message with cometD - Demo

Subscribe Event Message with cometD - Demo - event payload

Resources for demo

About CometD

Implemention of Bayeux protocol

Supported Methods

  • connect
  • disconnect
  • handshake
  • subscribe
  • unsubscribe

Bayeux

  • Protocol for transporting asynchronous messages
  • Primarily over web protocols such as HTTP and WebSocket
  • With low latency between a web server and web clients
  • Supports responsive bidirectional interactions between web client and the web server using AJAX

References

Platform Event Limits

Limits Doc

Video:Platform Events: Inside and Out