Whether you work on custom integrations or build near real-time apps, the Streaming APIs are there for you. We’ll give an overview of the four Streaming APIs, present what’s new for the APIs in the Summer ’19 Release, and provide you with a tool that lets you monitor those APIs.

The Four Streaming APIs

Did you know that there are multiple Streaming APIs? The original API was first introduced as “the” streaming API but it was only the first out of a family of four different APIs: Generic, PushTopic, Platform Events and Change Data Capture.


These four Streaming APIs are part of what we call the Enterprise Messaging Platform (EMP). The APIs were released in two batches over several years. Generic and PushTopic came as the “first generation,” then Platform Events and Change Data Capture came second. While the first generation of APIs is quite mature now, it is not evolving as much as the second one (more on that in the “What’s New” section of this post).

A shared model

All Streaming APIs are built on the same core technology: CometD. This technology relies on a messaging bus that offers a publish/subscribe mechanism for events (sometimes also referred to as messages).


The publish/subscribe model follows the following scenario:

  1. Event consumers connect to the messaging bus and subscribe to specific channels (event types)
  2. Event producers connect to the bus and publish an event on a channel
  3. The bus broadcasts the event to all registered subscribers

This means that the message producer is fully decoupled from the consumers. This model allows to build event-driven architectures at scale. Typical use cases for this are system integrations and real-time apps.

Distinct properties

These APIs are based on the same underlying technology but they have some distinct properties that make them suitable for specific use cases. Here’s an overview of the major differences — you can find a more detailed comparison in the documentation.

PushTopic Generic Platform Events Change Data Capture
Data structure Defined by target sObject retrieved via a SOQL query Unstructured (string) Custom typed fields Defined by target sObject
Publication Automated REST API Declarative or Code Automated
Subscription Code Declarative or Code

You’ll notice that APIs from the second generation (Platform Events and Change Data Capture) offer the most flexibility in terms of publication and subscription. Most notably, this new generation of APIs were introduced with powerful declarative capabilities and because of this, you should prioritize using them when possible.

If you need even more arguments in order to be convinced, here’s a recap of the different publication and subscription options that are offered for Platform Events and Change Data Capture:


Whether you are a coder or not, there are plenty of opportunities for you to explore these Streaming APIs.

What’s new in Summer ‘19

Among the new features introduced in the Summer ‘19 Release, there are a few notable improvements for the Streaming APIs.

lightning-emp-api Lightning web component

When the Streaming APIs came out there was no client natively available for the browser at first. Subscribing to streaming events in custom UI components would require a significant amount of code. That changed with the introduction of the Aura lightning:empAPI component. This awesome component manages CometD connections for you (authentication, connection multiplexing, etc) and greatly reduces the volume of code you need to write. You can read more about it in this post.

In Summer ’19, we now have an equivalent Lightning web component: the lightning-emp-api component. This component offers the same native subscribe capabilities as its Aura counterpart but is even easier to use because it only needs a couple of lines of JavaScript and requires no markup at all.

Platform Events updates

The most exciting update for Platform Events is the introduction of a configurable publish behavior.

By nature, Platform Events are published outside of transaction context. In other words if you have some Apex code that publishes an event and does some processing afterward, the event is published immediately and is not waiting for the Apex execution to finish. This means that the Apex execution might fail and the database transaction rolled back but the event is already sent and there is no way to cancel it.

With the introduction of the publish behavior settings you may now decide to delay the event publication after the Apex execution finishes.

Check out the release notes for other Platform Event updates.

Change Data Capture updates

There are two main updates to Change Data Capture (CDC): Apex trigger support and custom channels.

Just like for Platform Events, you may now write Apex triggers for CDC events. This brings extra flexibility compared to traditional object triggers because CDC triggers are executed outside of the scope of the database transaction that modified the data in first place.

The second remarkable update to CDC is the introduction of stream composition with custom channels. In a typical business scenario you would want to subscribe to multiple events like account, contact and order record changes. However, handling separate events in a coherent way is challenging because you likely need to write some code to aggregate them first. With custom channels, you may now regroup these events in a single custom event that you can handle. This means that you can easily process related changes in one go.

Check out the release notes for additional CDC updates.

Tooling: The Streaming Monitor

Implementing apps with the Streaming APIs without proper tooling can be hard due to the transient nature of streaming events. Past events are not saved to the database so there is no built-in user interface to look at logs. Due to that, we often end up wasting time to write some discardable code that traces those events for debugging purposes.

That is why I built and open sourced the Streaming Monitor, a tool that lets you monitor the four Streaming APIs and conveniently replay past events in a single click. Try it out on the AppExchange or get the code on GitHub. It will save you some valuable time.

Conclusion

This concludes our refresher on the four Streaming APIs. We’ve covered the different APIs, what they share and what are key differences. We’ve presented the latest updates and shared some tooling.

You can continue your streaming journey by learning more on Trailhead or by checking out these additional resources.

Resources

About the author

Philippe Ozil is Lead Developer Evangelist at Salesforce where he focuses on the Salesforce Platform. He writes technical content and speaks frequently at conferences. He is a full stack developer and a VR expert. Follow him on Twitter @PhilippeOzil or check his GitHub projects @pozil.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS