Sub topic 2
Topics and the kinds of events you can subscribe to
This chapter assumes you know what the Pub/Sub API is for and can read Python. It covers how you name the stream you want, and which kinds of events that stream can carry.
What a topic is
A topic is the named channel you subscribe to. Every Subscribe request carries a topic_name. The name is a path whose first segment tells you the event family. The rest identifies the specific event or object.
Subscribers never see events from an unrelated topic. One Subscribe stream reads one topic, so a service that needs several event types opens several streams.
Event families and their topic names
| Family | Topic name pattern | Example |
|---|---|---|
| Custom platform event | `/event/<Name>__e` | `/event/Order_Shipped__e` |
| Standard platform event | `/event/<Name>` | `/event/BatchApexErrorEvent` |
| Real-time event monitoring stream | `/event/<Name>Stream` | `/event/LoginEventStream` |
| Change Data Capture for one object | `/data/<Object>ChangeEvent` | `/data/AccountChangeEvent` |
| Change Data Capture for a custom object | `/data/<Name>__ChangeEvent` | `/data/Invoice__ChangeEvent` |
| Change Data Capture for all selected objects | `/data/ChangeEvents` | `/data/ChangeEvents` |
Check the exact names in your org. Your admin or developer defines custom platform events. Change Data Capture (CDC) only emits for objects that someone has selected for it in Setup.
The three kinds of events
- Platform events are messages your own code or automation defines and publishes. Apex, Flow, or an external client can publish them. They carry only the fields you define on the event.
- Change Data Capture events are generated by Salesforce when a record is created, updated, deleted, or undeleted. They describe what changed on a record. A later chapter covers them in detail.
- Real-time event monitoring events describe activity in the org, such as logins. Salesforce publishes them. Your org needs the right license or permission to receive them.
All three arrive through the same Subscribe call and use Avro payloads. What differs is the topic name and the schema behind it.
Checking a topic before you subscribe
A topic name that looks right can still fail. The name may have a typo, the event may not exist, or your user may lack permission. GetTopic tells you the truth before you open a stream. Its response includes can_subscribe and the schema_id you need to decode events.
The helper below sorts a topic name into a family. It also rejects names that cannot be topics, so a configuration mistake surfaces early.
def classify_topic(topic_name):
if not topic_name.startswith("/"):
raise ValueError("Topic names start with '/': " + topic_name)
parts = topic_name.split("/")
if len(parts) != 3 or not parts[2]:
raise ValueError("Expected /<prefix>/<name>: " + topic_name)
prefix, name = parts[1], parts[2]
if prefix == "data":
return "change_data_capture"
if prefix == "event":
if name.endswith("__e"):
return "custom_platform_event"
if name.endswith("Stream"):
return "real_time_event_monitoring"
return "standard_platform_event"
raise ValueError("Unknown topic prefix: " + prefix)
for topic in ["/event/Order_Shipped__e", "/data/AccountChangeEvent", "/event/LoginEventStream"]:
print(topic, "->", classify_topic(topic))This check is only a convenience for your own code. Salesforce decides whether a topic is valid, and GetTopic is the authoritative answer.
Common errors
- Wrong suffix.
/event/Order_Shippedfails where/event/Order_Shipped__eworks. Custom platform events need the__esuffix. - CDC not enabled. An
AccountChangeEventtopic can exist and still produce nothing. The object must be selected for Change Data Capture. - Missing permission. The authenticated user needs access to the event. A
can_subscribevalue of false, or a permission error, usually points here. - Assuming one schema. Each topic has its own schema ID. Do not reuse a decoder across topics.
Exercise
A teammate subscribes to /event/Invoice__ChangeEvent and gets an error. Which part of the name is likely wrong, and what should it be?
Show answer
Change Data Capture topics for custom objects start with /data/, not /event/. The correct name is /data/Invoice__ChangeEvent. Calling GetTopic on the corrected name should show can_subscribe as true, assuming the object is enabled for CDC and the user has access.