Connect with Me

LinkedIn

Read the full article at

https://dev.to/stevsharp/building-a-pubsub-system-in-net-masstransit-reactive-extensions-and-blockingcollection-2i6a

DotNet-PubSub-Examples

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.


πŸ“– Overview

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:

  1. MassTransit + RabbitMQ – a production-ready approach for distributed systems
  2. Reactive Extensions (Rx) – an in-memory, event-driven approach using observables
  3. BlockingCollection – a thread-safe producer/consumer approach inside a single process

πŸš€ Projects

1. MassTransitDemo

πŸ“‚ Path: /MassTransitDemo

  • Uses MassTransit with RabbitMQ
  • Publishes an OrderSubmitted event 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-management

RabbitMQ Management UI β†’ http://localhost:15672 (user: guest, pass: guest)

Run the demo:

cd MassTransitDemo
dotnet run
  1. 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 run

Sample output:

[Email] Send receipt to a@example.com [Analytics] Order ... VAT 35.76 ...

  1. BlockingCollectionDemo

πŸ“‚ Path: /BlockingCollectionDemo

Uses .NET BlockingCollection

Implements a thread-safe producer/consumer queue

Multiple subscribers consume messages concurrently

Run the demo:

cd BlockingCollectionDemo
dotnet run

Sample output:

[Email] Send receipt to user49.50@example.com
[Analytics] Track order ...

Connect with Me

LinkedIn