This repository contains three different implementations of the Publish/Subscribe (Pub/Sub) pattern in .NET.
The goal is to demonstrate how you can decouple publishers and subscribers using different tools, from in-memory solutions to distributed message brokers.
Publish/Subscribe (Pub/Sub) is a messaging pattern where a Publisher sends messages without knowing who receives them, and Subscribers consume those messages independently.
This enables:
- π Decoupling β publishers donβt need to know subscribers
- β‘ Scalability β multiple subscribers can process messages in parallel
- π οΈ Flexibility β new subscribers can be added without changing the publisher
This repo includes three implementations:
- MassTransit + RabbitMQ β a production-ready approach for distributed systems
- Reactive Extensions (Rx) β an in-memory, event-driven approach using observables
- BlockingCollection β a thread-safe producer/consumer approach inside a single process
π Path: /MassTransitDemo
- Uses MassTransit with RabbitMQ
- Publishes an
OrderSubmittedevent every few seconds - A consumer subscribes to the same event and logs received messages
Run RabbitMQ locally with Docker:
docker run -it --rm -p 5672:5672 -p 15672:15672 rabbitmq:3-managementRabbitMQ Management UI β http://localhost:15672 (user: guest, pass: guest)
Run the demo:
cd MassTransitDemo
dotnet run- ReactiveExtensionsDemo
π Path: /ReactiveExtensionsDemo
Uses System.Reactive
Demonstrates in-memory event streams with Subject
Multiple subscribers react to the same published messages
Run the demo:
cd ReactiveExtensionsDemo
dotnet runSample output:
[Email] Send receipt to a@example.com [Analytics] Order ... VAT 35.76 ...
- BlockingCollectionDemo
π Path: /BlockingCollectionDemo
Uses .NET BlockingCollection
Implements a thread-safe producer/consumer queue
Multiple subscribers consume messages concurrently
Run the demo:
cd BlockingCollectionDemo
dotnet runSample output:
[Email] Send receipt to user49.50@example.com
[Analytics] Track order ...