Skip to content

Design an sample application that allows for asynchronous announcement of events to multiple interested consumers, while avoiding tight coupling between the senders and receivers.

License

Notifications You must be signed in to change notification settings

mmrmilon/publishsubscribepattern

Repository files navigation

Publish and Subscribe Pattern

The Publish-Subscribe (Pub/Sub) pattern is a messaging pattern that allows different components or services of a system to communicate with each other without being directly aware of each other's existence. In this pattern, a message broker acts as an intermediary between publishers (senders) and subscribers (receivers) of messages.

The Pub/Sub pattern is commonly used in distributed systems, event-driven architectures, and microservices-based architectures. Some popular message brokers that implement the Pub/Sub pattern include Apache Kafka, RabbitMQ, and Google Cloud Pub/Sub.

Agenda

Implement the concept of Publish/Subscribe pattern. It should:-

  • Take an input of data
  • Transform that data in some way
  • Transport that data to a set of subscribers
  • The subscribers should be able to display the transformed data

Solutioning

To implement the Pub/Sub pattern based on our agenda, we will use RabbitMQ as a message broker. The .Net Core Web API will act as the publisher, and we will have at least two console applications serving as subscribers. As an addition to our implementation of the Pub/Sub pattern using RabbitMQ as the message broker, let me provide a part of real scenario from the current application I'm working on. For this scenario, we have three applications:

  • ResourceEngagement(publisher) - manages resource engagement information and publishes it to the subscribers
  • IdentityGovernance(subscriber) - consumes the published message and creates a resource profile in the organization's active directory
  • UserTimewrite(subscriber) - consumes the message to store engagement details and verify the resources' input hours.

Please find below the process diagram illustrating how pubsub is working: alt tag

Technologies

  • ASP.NET Core 7
  • Entity Framework Core 7
  • xUnit, Moq
  • RabbitMQ
  • Docker
  • Postman

Getting Started

The publisher project is configured to use an in-memory database by default. This ensures that all users will be able to run the solution without needing to set up SQL Server.

  • "UseInMemoryDatabase": true
  • We have database configuration in appsettings.json
  • We have RabbitMQ configuration in appsettings.json like host, user, password etc.

Running the Application

Since we have already implemented the publisher and subscribers applications based on our agenda and scenarios in this repository, you just need to clone it using git clone in your local machine, let's now look at how we can run them on our local machine.

  • Running RabbitMQ in Docker - Assuming that we have already set up Docker on our machine if not then find instructions from here

    • To spin up RabbitMQ server open command-line and use the docker run command as below

      docker run -d --hostname pubsub-rabbitmq-server --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management

    • We are using the rabbitmq:3-management image from DockerHub which will provide us a UI available on port 15672 to view our queues/messages.

    • In order to access the management UI, we need to open a browser and navigate to localhost:15672, using the default login of guest/guest.

  • Running Application - Ensuring we have both applications set to run on startup befor running applications.

    • Simply run by clicking on Start in visula studio, it will open a console two windows (Subscribers) and a browser with the Swagger UI (Publisher).
    • I'm using Postman here as publisher instead of using browser swagger.

Simulation Result

Step-01 - RabbitMQ management login UI alt tag Step-02 - RabbitMQ management dashboaed UI alt tag Step-03 - Publisher swagger UI alt tag Step-04 - Subscribers and Publisher (Postman) alt tag Step-05 - Create a engagement and published alt tag Step-06 - Create another engagement and published alt tag Step-07 - RabbitMQ queue dashboard alt tag Step-08 - RabbitMQ dashboard summary alt tag

About

Design an sample application that allows for asynchronous announcement of events to multiple interested consumers, while avoiding tight coupling between the senders and receivers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages