Files
nimmerworld.eachpath.local/learning/Publish-Subscribe.md
T

83 lines
2.5 KiB
Markdown

Publish-Subscribe
Everything in NATS starts with publish-subscribe. A publisher sends a message to a subject, and every subscriber listening on that subject gets a copy. This is the foundation everything else builds on.
How It Works
1.Publishers send messages to a subject — a simple string like orders.created
2.Subscribers express interest in subjects they care about
3.NATS delivers a copy of each message to every matching subscriber
4.No coupling — publishers don't know about subscribers, and subscribers don't know about publishers
This decoupling gives you tremendous flexibility. Services can be added, removed, or restarted without coordinating with anyone else.
Publishing Messages
You publish a message by sending it to a subject:
#!/bin/bash
# Publish a message to the "weather.updates" subject
nats pub weather.updates "Temperature: 72°F"
Python:
# Publish a message to the subject "weather.updates"
await nc.publish("weather.updates", "Temperature: 72°F".encode())
Key points:
1.Publishers don't wait for acknowledgments (fire-and-forget)
2.Messages are delivered to all active subscribers
3.If no subscribers exist, the message is simply discarded
Subscribing to Subjects
Subscribers express interest in subjects to receive messages:
#!/bin/bash
# Subscribe to messages on the "weather.updates" subject
nats sub "weather.updates"
Python:
# Subscribe to 'weather.updates' synchronously
sub = await nc.subscribe("weather.updates")
# Process messages
while True:
try:
msg = await sub.next(timeout=1)
print(f"Received: {msg.data.decode()}")
except TimeoutError:
break
When to Use Pub/Sub
Publish-subscribe is ideal when multiple services need to react to the same event:
1.Event broadcasting — notify services about user signups, order placements, deployments
2.Data distribution — send updates to multiple dashboards or monitoring services
3.Fan-out notifications — alert all interested parties about state changes
4.Audit logging — multiple services independently log the same events
Real-world example
Imagine an e-commerce system where an order is placed. You publish a single event:
nats pub orders.created '{"orderId": "123", "total": 99.99}'
Multiple services subscribe and each reacts differently:
1.Inventory Service updates stock levels
2.Email Service sends a confirmation email
3.Analytics Service records metrics
4.Shipping Service prepares the shipment
One publish, four independent reactions — no coordination needed.