Sub topic 2
Topics and the kinds of events you can subscribe to
This chapter assumes you can read Python, know what an OAuth 2.0 access token is, and understand the general idea of publish/subscribe messaging. You don't need to have run a gRPC call yet. Authentication and schemas come in later chapters.
What a topic is
A topic is a named channel on the Salesforce event bus. Publishers write events to it, and subscribers read events from it. In the Pub/Sub API, every call that reads events names a topic as a string, for example /event/Order_Shipped__e.
Topic names are case-sensitive and must be exact. A wrong name does not create a new topic. The call fails, as shown in the section on checking a topic with GetTopic.
The path prefix tells you what kind of event you are reading. That matters because the kind decides how events are produced, which fields they carry, and what you must configure in the org before anything arrives.
The three families you can subscribe to
The Pub/Sub API delivers three families of events.
| Family | Topic pattern | Example | What produces the events |
|---|---|---|---|
| Custom platform events | `/event/<Name>__e` | `/event/Order_Shipped__e` | Your own code or automation: Apex, flows, or API calls |
| Standard platform events and real-time event monitoring events | `/event/<Name>` | `/event/LoginEventStream` | Salesforce itself, when something happens in the org |
| Change Data Capture (CDC) events | `/data/<Name>ChangeEvent` | `/data/AccountChangeEvent` | Salesforce, when a record is created, updated, deleted, or undeleted |
Salesforce can add topics and rename them between releases. Treat the table as a guide to the naming pattern and check the current event reference for the exact list.
Custom platform events
A platform event is a message type you define in Setup, much like a custom object. The __e suffix marks it as a custom platform event. You choose the fields. Any publisher can then send events of that type: an Apex trigger, a flow, an external system calling the API, or another Pub/Sub client.
Platform events are not records. They are not stored in the database, you can't query them with SOQL, and you can't update them later. They exist on the event bus for a limited retention window, and subscribers read them from there.
Use a custom platform event when you want to announce something that happened, such as "payment cleared" or "device went offline". It fits best when the announcement does not map to one record change.
Standard platform events and real-time event monitoring
Salesforce publishes some events on its own. Real-time event monitoring is the best-known group. It streams security and usage activity, such as logins, API calls, report runs, and list view access. Topic names such as /event/LoginEventStream and /event/ApiEventStream belong to this group.
You don't define these events. You can only subscribe to them. Two conditions apply:
- The org must have the licence or permission that exposes the event.
- The connecting user must be allowed to read it.
If either is missing, GetTopic or Subscribe fails. An empty stream is a different case, and it is not an error.
Change Data Capture events
Change Data Capture publishes a change event whenever a record changes in an object you have enabled for it. A change event describes what changed. It is not a full copy of the record. The event header carries the entity name, the record IDs, and the type of change. The body carries the changed fields.
CDC has several topic shapes:
/data/AccountChangeEventcarries changes to one standard object./data/Invoice__ChangeEventcarries changes to one custom object. Note the__ChangeEventsuffix./data/ChangeEventscarries changes for every object you have selected for CDC, in one stream./data/<Channel>__chncarries changes for the objects you assigned to a custom channel.
Nothing flows until you select the object for CDC in Setup. The topic name can be valid and the stream still silent. A later chapter covers CDC in detail.
How the families differ in practice
The prefix is a reliable first test. Anything under /event/ is a message someone published on purpose, or a monitoring event Salesforce emits. Anything under /data/ reflects a change to stored data.
Compare them on the points that affect subscriber design:
- Who controls the schema. You do for custom platform events. Salesforce does for standard and CDC events. For CDC, the schema also follows the object, so a new field on Account appears in the Account change event schema.
- Whether the payload is complete. A platform event carries exactly the fields its publisher set. A CDC event carries only changed fields, so a subscriber that needs the full record must query for it or keep its own copy.
- What you must set up. Custom events need a definition. CDC needs the object selected. Monitoring events need the right licence.
- Who can publish. You can publish custom platform events from your own code. You can't publish a change event or a monitoring event. Salesforce does that.
Choosing a topic
Pick the family by asking what you are trying to learn.
- To learn that a business fact occurred, use a custom platform event. A flow or Apex class publishes it when the fact becomes true.
- To mirror record changes into another system, use CDC. You don't write publishing code.
- To feed a security or audit tool, use the real-time event monitoring topics.
One mistake is using a custom platform event to copy record changes when CDC already does that. Another is subscribing to /data/ChangeEvents and discarding most of it. Prefer a single-object topic or a custom channel when you only need a few objects.
The Pub/Sub API does not carry PushTopic events or generic streaming events. Those belong to the older Streaming API, which uses CometD. If you have a PushTopic today, moving to this API means switching to CDC or a platform event.
Checking a topic with GetTopic
You can ask the server about a topic before you subscribe. GetTopic returns whether you can subscribe to it and which schema ID its events use. The schema ID matters in a later chapter.
The code below reuses describe_topic from the chapter on authentication and adds a check for the failure cases that come from a mistyped or unavailable topic. It assumes you have already generated the pubsub_api_pb2 modules from the .proto file and obtained an access token.
import grpc
import pubsub_api_pb2 as pb2
import pubsub_api_pb2_grpc as pb2_grpc
ENDPOINT = "api.pubsub.salesforce.com:7443"
def check_topic(access_token, instance_url, tenant_id, topic_name):
metadata = (
("accesstoken", access_token),
("instanceurl", instance_url),
("tenantid", tenant_id),
)
with grpc.secure_channel(ENDPOINT, grpc.ssl_channel_credentials()) as channel:
stub = pb2_grpc.PubSubStub(channel)
try:
topic = stub.GetTopic(pb2.TopicRequest(topic_name=topic_name), metadata=metadata)
except grpc.RpcError as err:
print("GetTopic failed:", err.code().name, "-", err.details())
return None
if not topic.can_subscribe:
print(topic_name, "exists but this user cannot subscribe to it")
return None
print(topic_name, "is subscribable, schema_id =", topic.schema_id)
return topic.schema_idRun it against /event/Order_Shipped__e after you define that event, and again with a deliberately wrong case such as /event/order_shipped__e. The second call prints a gRPC error code and the server's message. Reading that message is the fastest way to tell a typo from a permissions problem.
A small guard against typos
Topic names are plain strings, so a typo reaches the server before anything catches it. A short local check catches the common mistakes earlier. The function below sorts a topic name into one of the families above.
def classify_topic(name):
if name.startswith("/event/"):
short = name[len("/event/"):]
if short.endswith("__e"):
return "custom platform event"
return "standard platform or monitoring event"
if name.startswith("/data/"):
short = name[len("/data/"):]
if short == "ChangeEvents":
return "CDC, all selected objects"
if short.endswith("__chn"):
return "CDC, custom channel"
if short.endswith("ChangeEvent"):
return "CDC, single object"
raise ValueError("Unrecognised topic name: " + name)
for topic in ["/event/Order_Shipped__e", "/data/AccountChangeEvent", "/data/ChangeEvents"]:
print(topic, "->", classify_topic(topic))This check does not prove the topic exists. It only rejects names that match no pattern, such as Order_Shipped__e without the /event/ prefix.
Common errors
- Using the API name without the prefix.
Order_Shipped__eis not a topic./event/Order_Shipped__eis. - Using the wrong CDC suffix. Standard objects use
AccountChangeEvent, but custom objects useInvoice__ChangeEvent. - Expecting a full record from CDC. Change events hold changed fields only.
- Subscribing to the wrong org. The access token, instance URL, and tenant ID must all belong to the org that owns the topic.
Exercise
A teammate wants to push every change to the Opportunity object into a data warehouse and plans to define a custom platform event called OpportunityChanged_e, published from an Apex trigger. Which topic would you suggest instead, what must be configured in the org before it delivers anything, and what limitation of its payload must the warehouse loader handle?
Show answer
Suggest Change Data Capture and subscribe to /data/OpportunityChangeEvent. Opportunity must be selected for Change Data Capture in Setup, or the stream stays empty. The payload holds only the changed fields plus a header with the record IDs and change type, so the loader must apply changes to its stored copy or query Salesforce for the full record. It cannot expect a complete row in every event.
Exercise
Classify each topic as a custom platform event, a standard or monitoring event, or a CDC topic: /event/LoginEventStream, /data/Invoice_ChangeEvent, /event/DeviceOffline__e.
Show answer
/event/LoginEventStream is a standard (real-time event monitoring) event. /data/Invoice_ChangeEvent is a CDC topic for the custom object Invoicec. /event/DeviceOffline_e is a custom platform event, shown by the _e suffix.