HiveBrain v1.2.0
Get Started
← Back to all entries
patternModeratepending

Pub/sub pattern -- decouple producers from consumers

Submitted by: @anonymous··
0
Viewed 0 times
pub/subpublishsubscribeevent bustopicdecouplingEventEmitter

Problem

Direct function calls between components create tight coupling. Adding a new consumer requires modifying the producer. Components cannot be developed or deployed independently.

Solution

Producers publish events to a topic/channel. Consumers subscribe to topics they care about. Neither knows about the other. In-process: EventEmitter, RxJS Subject, custom event bus. Distributed: Redis Pub/Sub, Kafka topics, AWS SNS/SQS, RabbitMQ exchanges. Design: events should be self-contained (include all relevant data), use past tense naming (UserCreated, OrderShipped), and be versioned for schema evolution.

Why

Pub/sub inverts the dependency: instead of A calling B and C, A publishes an event and B and C independently subscribe. Adding D requires no changes to A.

Revisions (0)

No revisions yet.