Sample Code - Async Queue implementation using RabbitMQ/.Net Core/ReactJS

Saurabh Singh
Feb 18 · 5 min read

Work Queue or Task Queue means the queue of tasks. We can implement various design patterns to distribute tasks in a queue to one or more workers.

Work queues makes more sense if the tasks involved are resource or time intensive. Idea is to avoid running such tasks immediately and having to wait for them to complete. Instead we schedule the tasks to be done later.

RabbitMQ or any other messaging framework has 3 main components:


Producer is the program that encapsulates the message and send to the queue.


Queue is like a large message buffer, depending on the configuration it it consumes host memory or disk space.

There is many-to-many relationship between producer and queue, means one producer can push to one or more queues and many producers can push to a single queue.


Consumer is a worker process that listens or receives messages from queue. Multiple consumers can listen from a single queue.

The producer, consumer, and queue do not have to reside on the same host; indeed in most applications they don’t. An application can be both a producer and consumer, too

Different Patterns

You can create any combination of 3 components, depending upon the work load, performance desired and business need.

one producer, one consumer
one producer, multiple consumers
one producer, multiple queue having single consumer each


Please refer following article to install and configure RabbitMQ

Demo code in .Net Core + ReactJS

You can clone the sample code from following github repository

The above code is only for reference to understand how you can push and receive messages from rabbitmq.

You can run the above program and check the queue functionality.

  • click SendToQ button to push messages to Queue.

You can more details in official RabbitMQ website:

The .NET client library

RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0–9–1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We’ll use the .NET client provided by RabbitMQ.

You can add “RabbitMQ.Client” nuget package.

Producer Code

The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us.

queue name: counter

exchange and routing key can be used if you plan to publish message to some particular queue. Exchange is kind interface that analyze routingkey attribute or other defined patterns and selects the appropriate to push the incoming message.

Consumer Code

In the following code we set the prefetchCount = 3. It means the consumer will keep only 3 messages in buffer at a time. Instead of count, you can also define the prefetchSize in bytes.

Message Acknowledgement — When a node delivers a message to a consumer, it has to decide whether the message should be considered handled (or at least received) by the consumer. Since multiple things (client connections, consumer apps, and so on) can fail, this decision is a data safety concern.

Depending on the acknowledgement mode used, RabbitMQ can consider a message to be successfully delivered either immediately after it is sent out (written to a TCP socket) or when an explicit (“manual”) client acknowledgement is received. Manually sent acknowledgements can be positive or negative and use one of the following protocol methods:

  • basic.ack is used for positive acknowledgements

Saurabh Singh

Written by

ReactJS, .Net Core, AWS, SQL, Docker, Mongo, RabbitMQ

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade