Architecture
Each node runs one topic actor (GoAktTopicActor). Local actors subscribe to and publish through their local topic actor. The topic actor maintains a registry of topics and their local subscribers.
Standalone mode
Publish to the local topic actor. The topic actor delivers the message to all local subscribers via Tell.
Cluster mode
In a cluster, each node has its own topic actor. Topic actors communicate with each other to disseminate messages. When a publish occurs:- The publisher sends
Publishto its local topic actor. - The local topic actor delivers to its local subscribers.
- The local topic actor sends a
TopicMessage(serialized) to each peer nodeβs topic actor via remoting. - Each remote topic actor receives the
TopicMessage, deserializes it, and delivers to its local subscribers.
When to use
- Decouple publishers from subscribersβpublishers do not need to know who is listening
- Broadcast application events (e.g. order created, inventory updated) to multiple actors
- Event-driven architectures where actors react to domain events
Enabling PubSub
The topic actor is spawned when either:WithPubSub()is passed when creating the actor system, or- Cluster mode is enabled (
WithCluster)
Topic actor
| Method | Purpose |
|---|---|
ActorSystem.TopicActor() | Returns the topic actorβs PID. Use to send Subscribe, Unsubscribe, Publish. |
GoAktTopicActor). From within an actor, use ctx.ActorSystem().TopicActor(). From outside (e.g. main), use system.TopicActor(). Returns nil if PubSub is not enabled.
Subscribe and Unsubscribe
Actors send messages to the local topic actor to subscribe or unsubscribe from topics:| Message | Constructor | Purpose |
|---|---|---|
Subscribe | actor.NewSubscribe(topic) | Subscribe this actor to a topic. Sender receives SubscribeAck on success. |
Unsubscribe | actor.NewUnsubscribe(topic) | Unsubscribe from a topic. Sender receives UnsubscribeAck on success. |
Publish
| Message | Constructor | Purpose |
|---|---|---|
Publish | actor.NewPublish(id, topic, message) | Publish a message to all subscribers of the topic. id is a unique message ID for deduplication. |
Receive invocationβthe payload is the published message, not a wrapper.
Cluster behavior
- Local delivery: The topic actor that receives the
Publishdelivers to its local subscribers immediately. - Remote dissemination: The topic actor sends a serialized
TopicMessageto each peerβs topic actor via remoting. Each peerβs topic actor deserializes and delivers to its local subscribers. - Deduplication: Uses sender ID, topic, and message ID to avoid processing the same message twice (e.g. when a topic actor receives a
TopicMessagefrom multiple paths).
Example
Event stream vs PubSub
| Aspect | Event stream | PubSub |
|---|---|---|
| Purpose | System and cluster observability | Application-level event distribution |
| Topics | Fixed internal topic | Application-defined topics |
| Publishers | Framework (PID, cluster, dead-letter) | Your actors |
| Subscribers | External (monitoring, logging) | Your actors |
| API | Subscribe() / Unsubscribe() | Messages to TopicActor() |
| Option | Always present | WithPubSub() or cluster |