Event-Driven Integrations Just Got Easier with the Pub/Sub API

The Pub/Sub API provides a unified interface for publishing and subscribing to platform events such as real-time event monitoring and change data capture. Pub/Sub API efficiently publishes and delivers binary event messages in the Apache Avro format using gRPC and HTTP/2.

Today, event-driven integrations have several publishing options, including the Bulk API, SOAP API, and REST API. We can use these APIs to publish any standard Platform Event that supports the create() call, as well as any custom Platform Event. We can use the Streaming API to subscribe to events, which allows us to subscribe to any Change Data Capture event, Platform Event, or Real-Time Event Monitoring event.

The new Pub/Sub API combines all of that functionality into a single, all-inclusive API. Using a single API, the Pub/Sub API enables users to publish events, subscribe to events, request schema, and request topical information.

The Pub/Sub API can assist you in streamlining your integration architecture and tackling projects such as:

↠ Subscribing to Change Data Capture events and syncing objects (Account, Contact, Opportunity, Order) data in an external system.

↠ Subscribing to a standard Platform Event like  and integrating with Google Calendar to update users’ calendars

↠ Subscribing to a standard Platform Event like AppointmentSchedulingEvent and integrating with Google Calendar to update users’ calendars

↠ Subscribing to Platform events and publishing a Platform Event back into Salesforce to update the data into Salesforce

↠ Subscribing to a standard Platform Event Process Exception Event to get errors that occur during payment processing (capture, apply, and refund) on an order summary. 

Advertisements

⤵️

The Pub/Sub API is a gRPC-based API, gRPC is a new industry-leading enterprise API protocol that offers numerous advantages over REST and SOAP:

  1. A binary framing protocol for data transport – unlike HTTP 1.1, which is text-based.
  2. Multiplexing support for sending multiple parallel requests over the same connection – HTTP 1.1 limits processing to one request/response message at a time.
  3. Bidirectional full-duplex communication for sending both client requests and server responses simultaneously.
  4. Built-in streaming enables requests and responses to asynchronously stream large data sets.
  5. Header compression reduces network usage.

gRPC is both lightweight and fast. It can be up to 8x faster than JSON serialization and produce messages that are 60-80% smaller.

gRPC is officially supported in 11 languages and has unofficial community support in additional languages, allowing developers with any mainstream programming background to fully utilize the Pub/Sub API’s capabilities.

Advertisements

⤵️

How to subscribe to Change Data Capture events in Node.js

This example is based on the GitHub repository pub-sub-api-node-client

In your Salesforce org: Goto > Setup> Search ‘Change Data Capture and select the object for that you want to capture the change event. For example, in the below screen, I have selected the Account object.

Click on the Save button.

Clone this GitHub repository on your local machine.

Create a .env file in the project’s root directory:

Advertisements

⤵️

SALESFORCE_LOGIN_URL = https://login.salesforce.com
SALESFORCE_USERNAME = YOUR_SALESFORCE_USERNAME
SALESFORCE_PASSWORD = YOUR_SALESFORCE_PASSWORD
SALESFORCE_TOKEN = YOUR_SALESFORCE_USER_SECURITY_TOKEN
PUB_SUB_ENDPOINT = api.pubsub.salesforce.com:7443
PUB_SUB_PROTO_FILE = pubsub_api.proto
PUB_SUB_TOPIC_NAME = /data/AccountChangeEvent
PUB_SUB_EVENT_RECEIVE_LIMIT = 10
view raw .env hosted with ❤ by GitHub

You need to change the event name if you are trying to implement another object.

PUB_SUB_TOPIC_NAME = /data/sObjectChangeEvent

If you want to subscribe Platform event then

PUB_SUB_TOPIC_NAME = /event/PlatformEventName ( Account_Update__e )
PUB_SUB_EVENT_RECEIVE_LIMIT = 10

You can put a limit on how many events you want to receive the script will terminate with these messages:

gRPC stream status:  {
  code: 0,
  details: '',
  metadata: Metadata { _internal_repr: {}, flags: 0 }
}
gRPC stream ended

Run the project with npm start

If everything goes well, the output will look like this:

Change any field on the Account object record in Salesforce, almost in real time it receives events, and it will display them as follows:

It’s worth noting that the event payload contains all object fields, but those that haven’t changed are null. To identify actual value changes, use the values from ChangeEventHeader.nulledFields,ChangeEventHeader.diffFields, and ChangeEventHeader.changedFields.

Summary

In the future, gRPC will gain traction for cloud-native systems. The advantages in performance and ease of development are compelling. REST, on the other hand, is likely to be around for a long time. It excels in terms of publicly exposed APIs and backward compatibility.

Advertisements
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s