<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amarnath Rao Sulake on Medium]]></title>
        <description><![CDATA[Stories by Amarnath Rao Sulake on Medium]]></description>
        <link>https://medium.com/@amarnathrao.sulake?source=rss-6ad9eed4bc5a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*i8JD6m5R9oSsSOSUgN-HwA.jpeg</url>
            <title>Stories by Amarnath Rao Sulake on Medium</title>
            <link>https://medium.com/@amarnathrao.sulake?source=rss-6ad9eed4bc5a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 00:54:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amarnathrao.sulake/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Notification Service using Node Js & RabbitMQ]]></title>
            <link>https://medium.com/@amarnathrao.sulake/notification-service-using-node-js-rabbitmq-71603a06033a?source=rss-6ad9eed4bc5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/71603a06033a</guid>
            <category><![CDATA[notification-service]]></category>
            <dc:creator><![CDATA[Amarnath Rao Sulake]]></dc:creator>
            <pubDate>Sat, 29 May 2021 17:35:39 GMT</pubDate>
            <atom:updated>2021-05-31T08:49:15.395Z</atom:updated>
            <content:encoded><![CDATA[<p>How to build your own notification service with Node js and<strong> </strong>RabbitMQ?</p><p>This article will help you to grasp how to build a notification service with the help of a message queue in node js.</p><p>Using a message queue is a no-brainer if you need to throttle the requests going to your API or workers, or you want things to happen asynchronously.</p><p>If you still want more reasons, please go through the following article.</p><p><a href="https://stackify.com/message-queues-12-reasons/">Message Queues &amp; You - 12 Reasons to Use Message Queuing</a></p><p>Some popular message queues are:</p><ul><li>RabbitMQ</li><li>Kafka</li><li>IBM MQ</li><li>ActiveMQ</li><li>RocketMQ</li><li>Qpid</li></ul><p>In this, we are using RabbitMQ.</p><p>RabbitMQ is the most popular open-source message broker. RabbitMQ is lightweight and easy to deploy. It supports multiple messaging protocols. RabbitMQ has decent performance and a strong community. If your requirement is to process thousands of messages per second, I would suggest you go for something like RabbitMQ.</p><p>To be honest, the RabbitMQ documentation is so well written (<a href="https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html">https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html</a>), you don’t need anything else. But we need a RabbitMQ tutorial as part of this series. So I will be summarizing the documentation to some extent and see how we can build a REST API to work with RabbitMQ drivers.</p><p>A normal work queue setup in RabbitMQ looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/0*EeZl6t-daNMmK2AO" /></figure><p><strong>Work Queues set up in RabbitMQ</strong></p><p>There are 3 important parts to the RabbitMQ setup:</p><ol><li>Producer. A program that sends messages is a producer. (Represented by P in the diagram).</li><li>Queue. Messages are stored inside a queue. It’s essentially a large message buffer. A queue is only bound by the host’s memory and disk limits. (Represented by the red rectangular boxes in the diagram).</li><li>Consumer. A consumer is a program that mostly waits to receive messages. (Represented by C1 &amp; C2 in the diagram).</li></ol><p>There can be multiple producers and consumers in a queue. But in normal use cases, we don’t need multiple producers, as putting messages into a queue is quite simple and fast. But consuming a message from the queue and performing a task based on the message normally takes time. Thus we tend to implement multiple consumers for a queue so that when a consumer is busy, the other consumer can read from the queue and perform the task.</p><p>Doing a task can take a few seconds. You may wonder what happens if one of the consumers starts a long task and dies with it only partly done. In this case, if the worker crashes, we will lose the message it was just processing. In order to make sure a message is never lost, RabbitMQ supports message acknowledgments.</p><p>An ack(acknowledgment) is sent back by the consumer to tell RabbitMQ that a particular message has been received, processed and that RabbitMQ is free to delete it. RabbitMQ will understand that a message wasn’t processed fully and will re-queue it. If there are other consumers online at the same time, it will then quickly redeliver it to another consumer.</p><p>But what if the RabbitMQ server crashes? Will the messages in the queue get lost?</p><p>By default, yes. But there are steps to ensure your queue and messages are persistent to your disk. So that, in the case of a server crash, you can restart your server and process the messages.</p><p>First, we have to ensure our queue is declared durable. We can either do that in the RabbitMQ admin console or through code. Then we should ensure the messages in the queue are persistent by setting the persistent field as true</p><p>We can have a RabbitMQ set up on our local machine by installing RabbitMQ, or you can run a RabbitMQ as a container in a docker.</p><p>Once setup is done, we are good to use RabbitMQ admin console where you can get all information regarding Queues, Exchanges, Connections, Channels etc.</p><p>Messages are not published directly to a queue. Instead, the producer sends messages to an exchange.</p><p><strong>Exchanges </strong>are message routing agents, defined by the virtual host within RabbitMQ.</p><p>An exchange is responsible for routing the messages to different queues with the help of header attributes, bindings, and routing keys.</p><p>A <strong>binding </strong>is a “link” that you set up to bind a queue to an exchange.</p><p>RabbitMQ documentation is so well written in detail regarding exchanges and their types. (<a href="https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html">https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html</a>)</p><p>You can add or delete Queue/Exchange in the console itself.</p><p>Ideally, we should have a producer and consumers which will have the relationship based on the Queues.</p><p>In simple words the producer will put the message to Queue in RabbitMQ, the consumer listening to the respective Queue will consume the message and later it will perform the respective tasks.</p><p><strong>In the concept of Notification service:-</strong></p><p>Let me brief in simple words</p><p>Client(reactjs app) — → Server(Nodejs)</p><p>Notification Server(Nodejs)</p><p>For understanding how the consumed message or notification will get to the client we have to first know about the concept of sockets and how it works.</p><p><strong>WebSocket?</strong></p><p>WebSockets can be regarded as the future of client-server communication. In WebSockets, there would be one TCP connection between client &amp; server at all times. There’s the bi-directional data flow between client &amp; server as well as real-time nature due to always open TCP connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/0*uRJYpRct0f7eJf83" /></figure><p><strong>An Introduction to Socket.io:-</strong></p><p><strong>What is Socket.io?</strong></p><p>Socket.io is a cross-browser JavaScript library that provides event-driven communication capabilities for use in real-time applications. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Both components have an identical API.</p><p><strong>Initialize Connection</strong></p><p>To begin with I need to install the npm package by npm install socket.io. The client can use client code from the existing socket.io package, use CDN Javascript file, or client-based socket.io-client. The server is based on Express in this case but plain NodeJS code is there in the documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iBE9Qy13G4rRXkC5" /></figure><p>In this instance when we simply connect to localhost:3000 in the browser it can be seen in the running server instance as ‘a user connected’. Initialization of connection is simple as that.</p><p><strong>Bidirectional Communication</strong></p><p>Communication between client and server is even simpler. The sender would have to socket.emit, while the client can listen on socket.on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Iw3OhFDn2areuAd6" /></figure><p>Here the channel is a custom_event which either client or server can emit (send) or on (receive).</p><p>I hope from the above I was able to give a brief idea about WebSockets &amp; SocketIO.</p><p>Now, will come to our scenario where the client and servers will communicate through the socket connection in order to implement the notifications.</p><p>Assume that we have a client written in ReactJS, a server in NodeJS(REST APIS) that will deal with the data for storing and retrieving, and a notification server in NodeJS.</p><p><strong>OUR APP CLIENT</strong>:- We have our UI which is developed using ReactJS. I am not going in-depth on how we have components in the application and am focusing on how we will implement the notifications.</p><p>We have to maintain a socket connection with the notification server in order to get the real time notifications.</p><p><strong>OUR APP SERVER</strong>:- We have our server, which will take care of all application flow through the REST APIS. Also, the Publisher will be there which will in response push or publish the messages/notifications to the message queue.</p><p><strong>NOTIFICATION SERVER:- </strong>We can say in other words a notification server is a consumer which always listens to the binded queue of RabbitMQ. As I already explained in the client a socket connection has to be established between the client and notification server.</p><p>Soon when the publisher (Our App Server) pushes/publishes a message to queue then the consumer (Notification server) will consume and will transmit through the socket connection.</p><p>I hope from the above I was able to give a brief idea about how real-time notifications work.</p><p>Let me give a sample code for the publisher and consumer.</p><p><strong>The amqp.node client library</strong></p><p>We’ll use the amqp.node client.</p><p>First, install amqp.node using npm: <strong>npm install amqplib</strong></p><p><strong>var amqp = require(‘amqplib/callback_api’);</strong></p><p>then connect to the RabbitMQ server</p><p><strong>Publisher:-</strong></p><pre>amqp.connect(‘amqp://localhost’, function(error0, connection) {});</pre><pre>amqp.connect(‘amqp://localhost’, function(error0, connection) {</pre><pre>if (error0) {</pre><pre>throw error0;</pre><pre>}</pre><pre>connection.createChannel(function(error1, channel) {});</pre><pre>});</pre><h4>To send, we must declare a queue for us to send to; then we can publish a message to the queue:</h4><pre>amqp.connect(‘amqp://localhost’, function(error0, connection) {</pre><pre>if (error0) {</pre><pre>throw error0;</pre><pre>}</pre><pre>connection.createChannel(function(error1, channel) {</pre><pre>if (error1) {</pre><pre>throw error1;</pre><pre>}</pre><pre>var queue = ‘hello’;</pre><pre>var msg = ‘Hello world’;</pre><pre>channel.assertQueue(queue, {</pre><pre>durable: false</pre><pre>});</pre><pre>channel.sendToQueue(queue, Buffer.from(msg));</pre><pre>console.log(“ [x] Sent %s”, msg);</pre><pre>});</pre><pre>});</pre><p><strong>Consumer:-</strong></p><p>Setting up is the same as the publisher; we open a connection and a channel and declare the queue from which we’re going to consume. Note this matches up with the queue that <strong>sendToQueue </strong>publishes to.</p><pre><strong>amqp.connect(‘amqp://localhost’, function(error0, connection) {</strong></pre><pre><strong>if (error0) {</strong></pre><pre><strong>throw error0;</strong></pre><pre><strong>}</strong></pre><pre><strong>connection.createChannel(function(error1, channel) {</strong></pre><pre><strong>if (error1) {</strong></pre><pre><strong>throw error1;</strong></pre><pre><strong>}</strong></pre><pre><strong>var queue = ‘hello’;</strong></pre><pre><strong>channel.assertQueue(queue, {</strong></pre><pre><strong>durable: false</strong></pre><pre><strong>});</strong></pre><pre><strong>});</strong></pre><pre><strong>});</strong></pre><p>Note that we declare the queue here, as well. Because we might start the consumer before the publisher, we want to make sure the queue exists before we try to consume messages from it.</p><p>We’re about to tell the server to deliver us the messages from the queue. Since it will push us messages asynchronously, we provide a callback that will be executed when RabbitMQ pushes messages to our consumers. This is what <strong>Channel.consume</strong> does.</p><pre><strong>console.log(“ [*] Waiting for messages in %s. To exit press CTRL+C”, queue);</strong></pre><pre><strong>channel.consume(queue, function(msg) {</strong></pre><pre><strong>console.log(“ [x] Received %s”, msg.content.toString());</strong></pre><pre><strong>}, {</strong></pre><pre><strong>noAck: true</strong></pre><pre><strong>});</strong></pre><p>The above is the sample code for the consumer and publisher. This is how we can build the notification service.</p><p>As this is my first article I tried to explain how to build the notification service. Hope you will get a basic understanding of how to build a notification service using NodeJs and RabbitMQ.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71603a06033a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>