Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
Publish-Subscribe is a messaging pattern where:
This simple but powerful decoupling is why systems like
...all handle complexity effortlessly.
Why It Matters Today?
eventBus.subscribe('order_created', applyLoyaltyPoints);Example of E-commerce:
function processOrder() { chargeCard(); // Payment updateInventory(); // Warehouse sendConfirmation(); // Email // Adding analytics? Must modify this function! }function processOrder() { eventBus.publish('order_processed', orderData); // That's it! }paymentService.subscribe('order_processed', chargeCard); warehouseService.subscribe('order_processed', updateInventory); // Add analytics later without touching processOrder()Here's why it's so powerful:
1. Loose Coupling
2. Asynchronous & Ordered Handling
3. Cleanup-Friendly
Unsubscribe when done (e.g., when a React component unmounts) to avoid memory leaks or duplicate handlers.
Real-World Examples
button.addEventListener('click', onClick) // subscribe button.removeEventListener('click', onClick) // unsubscribeserver.on('connection', socket => { … }) // each new client socket.emit('data', chunk) // push data to listeners// analytics.js emitter.subscribe('purchase', data => sendToAnalytics(data)); // cart.js emitter.emit('purchase', { itemId: 123, price: 29.99 });The Magazine Analogy
How It Works:
Domain-Level Pub/Sub in Your App
Imagine an e-commerce app where an order triggers:
1. Without Pub/Sub (Tightly Coupled)
import { sendConfirmationEmail } from './email'; import { updateInventory } from './inventory'; import { recordAnalytics } from './analytics'; import { notifyShipping } from './shipping'; function placeOrder(orderData) { // Core order logic const order = createOrderInDatabase(orderData);sendConfirmationEmail(order); // Email dependency updateInventory(order.items); // Inventory dependency recordAnalytics('purchase', order); // Analytics dependency notifyShipping(order); // Shipping dependency return order; }If your placeOrder() function directly called each of those modules, you’d end up with tangled imports and hard-to-test code.
Instead you can:
2. With Pub/Sub (Decoupled)
import emitter from './eventEmitter'; function placeOrder(orderData) { const order = createOrderInDatabase(orderData); emitter.emit('orderPlaced', order); // Just emit an event return order; }----------------------------
Listeners (Subscribers)
----------------------------
emitter.subscribe('orderPlaced', (order) => { sendConfirmationEmail(order); });emitter.subscribe('orderPlaced', (order) => { updateInventory(order.items); });emitter.subscribe('orderPlaced', (order) => { recordAnalytics('purchase', order); });emitter.subscribe('orderPlaced', (order) => { notifyShipping(order); });What Are Unrelated Services?
Unrelated services are modules or functions that:
Rule of Thumb
Before:
function doSomething() { taskA(); // Unrelated to B/C taskB(); // Unrelated to A/C taskC(); // Unrelated to A/B }After:
function doSomething() { emitter.emit('somethingDone', data); } // Elsewhere: emitter.subscribe('somethingDone', taskA); emitter.subscribe('somethingDone', taskB); emitter.subscribe('somethingDone', taskC);This keeps your code flexible, testable, and maintainable.