<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arda Örkin</title>
    <description>The latest articles on DEV Community by Arda Örkin (@ardaorkin).</description>
    <link>https://dev.to/ardaorkin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F337000%2Fca5ad39a-c79c-4d9e-9ff8-8ec2e9ab229c.jpeg</url>
      <title>DEV Community: Arda Örkin</title>
      <link>https://dev.to/ardaorkin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ardaorkin"/>
    <language>en</language>
    <item>
      <title>Parcel Tracking Application With RabbitMQ</title>
      <dc:creator>Arda Örkin</dc:creator>
      <pubDate>Fri, 14 May 2021 20:20:39 +0000</pubDate>
      <link>https://dev.to/ardaorkin/parcel-tracking-application-with-rabbitmq-7bo</link>
      <guid>https://dev.to/ardaorkin/parcel-tracking-application-with-rabbitmq-7bo</guid>
      <description>&lt;p&gt;In the development process, besides specify which technologies are going to be used, it is important to determine how the project architecture is going to be. These days, the most common project architecture is microservice architecture. One of the basic features of the microservice architecture is that every service can be deployed separately. Thanks to this feature, every single service can use a different protocol(s) in the project. It means a project can contain both HTTP, AMQP, and WS connection protocols.&lt;/p&gt;

&lt;p&gt;In this article, we are going to build a service that uses both HTTP, AMQP, and WS. In this service we are going to use these technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express.js&lt;/li&gt;
&lt;li&gt;MongoDB&lt;/li&gt;
&lt;li&gt;Socket.IO&lt;/li&gt;
&lt;li&gt;RabbitMQ&lt;/li&gt;
&lt;li&gt;ReactJS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will use the Express.js library to handle HTTP requests. When HTTP requests are received we will execute &lt;em&gt;publishers&lt;/em&gt; which publish a message about the event to the RabbitMQ message broker. We will also create consumers to listen to the messages which are sent to the RabbitMQ message broker by the publishers, and save them to the MongoDB database. At last, we will use Socket.IO in order to track every change in MongoDB and to provide rendering those changes in the frontend built by using React, without refreshing the page.&lt;/p&gt;

&lt;p&gt;Here is the final code of the &lt;a href="https://github.com/ardaorkin/rabbitmq-parcel-tracking.git"&gt;project&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;NodeJS 12v+&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Building Development Environment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Server
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First of all, let's create a folder called &lt;strong&gt;parcel-tracking-system&lt;/strong&gt;. In that folder, let's create one more folder called &lt;strong&gt;server&lt;/strong&gt;. Then open up a terminal and go to the directory where our server folder in you just created and run &lt;strong&gt;npm init -y&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then go back again to the terminal and to install project dependencies run this command in the directory where the folder named &lt;strong&gt;server&lt;/strong&gt; is in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express dotenv tortoise mongoose socket.io nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To ensure that command was executed successfully and packages were installed both look at the message returned in the terminal and open up the package.json file and look dependencies section.&lt;/p&gt;

&lt;p&gt;Also, let's install development environment dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save-dev @babel/core @babel/preset-env babel-loader
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later on, create a file named &lt;strong&gt;.babelrc&lt;/strong&gt; and write this code in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{                                        
    "presets": ["@babel/env"]                                        
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create one more file named &lt;strong&gt;server.js&lt;/strong&gt; and write this code in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//server.js

import express from "express"              

const app = express()              

app.use("/", (req, res) =&amp;gt; {                

res.send("Welcome to parcel tracking system")              

})              

app.listen(8000, () =&amp;gt; console.log(`Server listening on 8000`))    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute &lt;strong&gt;server.js&lt;/strong&gt; with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./server --exec babel -e js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the execution, open &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; in a browser. If the "Welcome to parcel tracking system" message comes up on the page, it means Express.js installation has successfully ended.&lt;/p&gt;

&lt;h3&gt;
  
  
  Publisher and Consumer Creation
&lt;/h3&gt;

&lt;p&gt;In these steps, we are going to create our first publisher and consumer. Publisher will publish a message, send it to &lt;em&gt;exchange&lt;/em&gt; where is in RabbitMQ message broker and the &lt;em&gt;exhange&lt;/em&gt; will send the message to the message queue and the &lt;em&gt;consumer&lt;/em&gt; which listens to the messages which are it cares in the queue, will take the message and will do what we want to do.&lt;/p&gt;

&lt;p&gt;For now, we will log to the console the message which we get via the consumer. To do that let's create two folders named &lt;strong&gt;publisher&lt;/strong&gt; and &lt;strong&gt;consumer&lt;/strong&gt; under the folder called &lt;strong&gt;server&lt;/strong&gt;. In the &lt;strong&gt;publisher&lt;/strong&gt; folder, create a file named &lt;strong&gt;shippingPublihser.js&lt;/strong&gt; and in the consumer folder, create a file named &lt;strong&gt;shippingConsumer.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Write this code down to the &lt;strong&gt;shippingPublihser.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shippingPublisher.js

import Tortoise from "tortoise"          
import dotenv from "dotenv";          
dotenv.config()          

const tortoise = new Tortoise(process.env.AMQP_URL)    
        tortoise      
            .exchange("parcel-tracking", "topic", { durable: false })      
            .publish("parcel.shipping", { name: "test", status: "shipping" });        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's write the consumer which is going to take the message which is sent to the message broker by the publisher. To do this write this code down to the &lt;strong&gt;shippingConsumer.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shippingConsumer.js

import Tortoise from "tortoise"                            
import dotenv from "dotenv";                            
dotenv.config()                            

const tortoise = new Tortoise(process.env.AMQP_URL)                            

tortoise  
    .queue("", { durable: false })  
    .exchange("parcel-tracking", "topic", "*.shipping", { durable: false })  
    .prefetch(1)  
    .json()  
    .subscribe((msg, ack, nack) =&amp;gt; {    
        console.log(msg)    
        ack();  
    });            
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can prefer to install RabbitMQ on our local machine but in that case, the installation steps would be different from the operating system to the operating system and we would need to mess with some network settings. Therefore, we make this step with &lt;a href="http://cloudamqp.com"&gt;http://cloudamqp.com&lt;/a&gt;. To do that let's create &lt;a href="https://cloudamqp.com"&gt;https://cloudamqp.com&lt;/a&gt;. Later on, click on the button &lt;strong&gt;Create New Instance&lt;/strong&gt; and create a new message broker instance. We can name the instance as we wish. To free plan, choose the Little Lemur option. Then press the buttons named &lt;strong&gt;Select Region&lt;/strong&gt; &amp;gt; &lt;strong&gt;Review&lt;/strong&gt; &amp;gt; &lt;strong&gt;Create Instance&lt;/strong&gt; respectively. Go to the &lt;a href="https://customer.cloudamqp.com/instance"&gt;page&lt;/a&gt; where message brokers are listed and click on the name of the message broker instance we just created. Copy the value of &lt;strong&gt;AMQP URL&lt;/strong&gt; in the &lt;strong&gt;Details&lt;/strong&gt; section where is in the page came up. After applying these steps, go back to the &lt;strong&gt;server&lt;/strong&gt; folder is in and creates a file named &lt;strong&gt;.env&lt;/strong&gt;. Write these lines down in this &lt;strong&gt;.env&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AMQP_URL="&amp;lt;copied_amqp_url&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By this process, we create an AQMP service. Now it is time to run our publisher and consumer. To do this, in two separate terminals, run these commands respectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./consumers/shippingConsumer --exec babel-node -e js          
nodemon ./publishers/shippingPublisher --exec babel-node -e js        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After applying these steps, we should see this message in the terminal where &lt;strong&gt;shippingConsumer.js&lt;/strong&gt; is running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ name: 'test', status: 'shipping' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this time we have a working service! This service is using AMQP as a communication protocol. The service has two ends; one is a publisher, and the other is a consumer. The publisher sends a message to the message broker after this process publisher's job is done and it doesn't wait for any reply. The consumer just cares about what the message header includes. In that case, the consumer cares about messages which have &lt;em&gt;shipping&lt;/em&gt; statement in their header. There are nothing consumer needs except the message header.&lt;/p&gt;

&lt;p&gt;In the next step, we are going to publish messages when an HTTP request is received.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling HTTP Requests
&lt;/h3&gt;

&lt;p&gt;Let's create two more each publisher and consumer. Again to the &lt;strong&gt;server&lt;/strong&gt; folder. Create these files in the &lt;strong&gt;publishers&lt;/strong&gt; folder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;onroadPublisher.js&lt;/li&gt;
&lt;li&gt;deliveredPublisher.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And create these files in the &lt;strong&gt;consumers&lt;/strong&gt; folder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;onroadConsumer.js&lt;/li&gt;
&lt;li&gt;deliveredConsumer.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we are going to write all publisher files as &lt;em&gt;Promises&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shippingPublisher.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            

const shippingPublisher = (name) =&amp;gt;              
    new Promise((resolve, reject) =&amp;gt; {                
        tortoise                  
            .exchange("parcel-tracking", "topic", { durable: false })                  
            .publish("parcel.shipping", { name, status: "shipping" });                  
        resolve({ name, status: "shipping" });              
});            

export default shippingPublisher;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//onroadPublisher.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            

const onroadPublisher = (name) =&amp;gt;              
    new Promise((resolve, reject) =&amp;gt; {                
        tortoise                  
            .exchange("parcel-tracking", "topic", { durable: false })                  
            .publish("parcel.onroad", { name, status: "onroad" });                  
        resolve({ name, status: "onroad" });              
});            

export default onroadPublisher;     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//deliveredPublisher.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            

const deliveredPublisher = (name) =&amp;gt;              
    new Promise((resolve, reject) =&amp;gt; {                
        tortoise                  
            .exchange("parcel-tracking", "topic", { durable: false })                  
            .publish("parcel.delivered", { name, status: "delivered" });                  
        resolve({ name, status: "delivered" });              
});            

export default deliveredPublisher;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later on, let's code consumers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shippingConsumer.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.shipping", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe((msg, ack, nack) =&amp;gt; {                
        console.log(msg)                
    ack();              
    });      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//onroadConsumer.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.onroad", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe((msg, ack, nack) =&amp;gt; {                
        console.log(msg)                
    ack();              
    });     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//deliveredConsumer.js

import Tortoise from "tortoise";            
import dotenv from "dotenv";            
dotenv.config();            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.delivered", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe((msg, ack, nack) =&amp;gt; {                
        console.log(msg)                
    ack();              
    });      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these configurations, as the server receives HTTP requests, publishers will be executed and as these publishers send messages to the message broker, our consumers are going to listen to these messages.&lt;/p&gt;

&lt;p&gt;Now let's create HTTP routes. For the sake of best practice and clean code create a folder named &lt;strong&gt;routes&lt;/strong&gt; under the &lt;strong&gt;server&lt;/strong&gt; folder and create a file named &lt;strong&gt;index.js&lt;/strong&gt; in it. Later write these lines down to the &lt;strong&gt;index.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.js

import { Router } from "express";            
import shippingPublishers from "../publishers/shippingPublisher";            
import onroadPublisher from "../publishers/onroadPublisher";            
import deliveredPublisher from "../publishers/deliveredPublisher";            

const router = Router();            

router.get("/", (req, res) =&amp;gt; {              
    res.send("Welcome to parcel-tracking system");            
});            

router.get("/shipping/:name", async (req, res, next) =&amp;gt; {              
    const name = req.params.name;              
    await shippingPublishers(name).then((message) =&amp;gt; res.json(message));            
});            

router.get("/onroad/:name", async (req, res, next) =&amp;gt; {              
    const name = req.params.name;              
    await onroadPublisher(name).then((message) =&amp;gt; res.json(message));            
});            

router.get("/delivered/:name", async (req, res, next) =&amp;gt; {              
    const name = req.params.name;              
    await deliveredPublisher(name).then((message) =&amp;gt; res.json(message));            
});            

export default router;      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating HTTP routes, let's modify all &lt;strong&gt;server.js&lt;/strong&gt; file like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//server.js

import express from "express";
import router from "./routes";            

const app = express();
const port = process.env.PORT || 8000;

app.use(router);

app.listen(port, () =&amp;gt; console.log(`Server listening on port ${port}`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later on, go to under the &lt;strong&gt;server&lt;/strong&gt; folder and run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./server --exec babel-node -e js            
nodemon ./consumers/shippingConsumer --exec babel-node -e js            
nodemon ./consumers/onroadConsumer --exec babel-node -e js            
nodemon ./consumers/deliveredConsumer --exec babel-node -e js   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all, commands run, open these links up in a browser respectively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/shipping/test"&gt;http://localhost:8000/shipping/test&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/onroad/test"&gt;http://localhost:8000/onroad/test&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/delivered/test"&gt;http://localhost:8000/delivered/test&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we open up the links, we should see messages which go to the message broker, in the terminal in JSON format.&lt;/p&gt;

&lt;p&gt;After seeing both the terminal where consumers are running on and the JSON data which are returned in the browser, we can be sure that both publishers and consumers are running as the server receives HTTP requests.&lt;/p&gt;

&lt;p&gt;In the next step, we are going to save the messages to MongoDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  MongoDB Configuration
&lt;/h3&gt;

&lt;p&gt;To configure MongoDB, let's create an account in &lt;a href="https://www.mongodb.com/cloud"&gt;https://www.mongodb.com/cloud&lt;/a&gt;. Later on create an organization, a project in the organization, and a cluster in the project. After the cluster is created, in the page where the cluster is in, click &lt;strong&gt;Database Access&lt;/strong&gt;. Click &lt;strong&gt;ADD NEW DATABASE USER&lt;/strong&gt; choice there and create a database user. Later on, click the &lt;strong&gt;Cluster&lt;/strong&gt; option on the left bar. On the opened page, click the &lt;strong&gt;Connect&lt;/strong&gt; button. In &lt;strong&gt;Setup connection security&lt;/strong&gt; step, choose &lt;strong&gt;Allow anywhere&lt;/strong&gt; option and click &lt;strong&gt;Choose a connection method&lt;/strong&gt;. Then let's click the &lt;strong&gt;Connect your application&lt;/strong&gt; option and copy the connection information under &lt;strong&gt;Add your connection string into your application code&lt;/strong&gt;. Let's go back to the text editor and create a variable named &lt;strong&gt;MONGODB_URL&lt;/strong&gt; in the &lt;strong&gt;.env&lt;/strong&gt; file and assign the MongoDB connection information to the variable. Change &lt;strong&gt;&lt;/strong&gt; with the password just created in &lt;strong&gt;Database Access&lt;/strong&gt; step and change &lt;strong&gt;myFirstDatabase&lt;/strong&gt; as &lt;strong&gt;parceltracking&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URL="mongodb+srv://username:12345@cluster0.mjh9d.mongodb.net/parceltracking?retryWrites=true&amp;amp;w=majority"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these steps, go to &lt;strong&gt;server.js&lt;/strong&gt; file and write down these lines just after the line where the routes folder is imported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//server.js

import mongoose from "mongoose"
import dotenv from "dotenv"
dotenv.config()              
mongoose.connect(process.env.MONGODB_URL, {              
    useNewUrlParser: true,              
    useUnifiedTopology: true,            
});            

const db = mongoose.connection;            
db.on("error", console.error.bind(console, "connection error:"));            
db.once("open", () =&amp;gt; console.log("Connected to database"));      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With these lines, we opened up a MongoDB connection. To be sure that everything is okay, let's run &lt;strong&gt;server.js&lt;/strong&gt; with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./server --exec babel-node -e js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we see the message &lt;strong&gt;Connected to database&lt;/strong&gt; in the console of our terminal, it means we did the database configuration correctly. In the next step, we are going to create a MongoDB schema and model. To do that, let's create a folder named &lt;strong&gt;model&lt;/strong&gt; in the &lt;strong&gt;server&lt;/strong&gt; folder. In the &lt;strong&gt;model&lt;/strong&gt; folder, create a file named &lt;strong&gt;Tracking.js&lt;/strong&gt; and write this code down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Tracking.js

import mongoose from "mongoose";

const trackingSchema = new mongoose.Schema({              
    name: String,              
    status: String,            
});            

const Track = mongoose.model("Track", trackingSchema);            

export default Track;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the model, we will use it for consumers. But while we use the save() method from the &lt;strong&gt;mongoose&lt;/strong&gt; library, the updateOne() method is going to be used in other consumers. First of all, let's modify the &lt;strong&gt;shippinConsumer.js&lt;/strong&gt; file like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shippingConsumer.js

import Tortoise from "tortoise";            
import mongoose from "mongoose"            
import Track from "../model/Tracking";            
import dotenv from "dotenv"            
dotenv.config()                          
mongoose.connect(process.env.MONGODB_URL, {                          
    useNewUrlParser: true,                          
    useUnifiedTopology: true,                        
});                        

const db = mongoose.connection;                        
db.on("error", console.error.bind(console, "connection error:"));                        
db.once("open", () =&amp;gt; console.log("Connected to database"));            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.shipping", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe((msg, ack, nack) =&amp;gt; {                
        const newParcel = new Track(msg);                
        newParcel.save((err, parcel) =&amp;gt; {                  
            if (err) throw err;                  
            console.log("shipped parcel:", parcel);                  
            return parcel;                  
        });                
        ack();              
    });            
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, &lt;strong&gt;shippinConsumer.js&lt;/strong&gt; will create a new record in MongoDB. Now, it is time to update this record. To do that let's modify onroadConsumer and deliveredConsumer like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//onroadConsumer.js

import Tortoise from "tortoise";              
import mongoose from "mongoose"              
import Track from "../model/Tracking";              
import dotenv from "dotenv"              
dotenv.config()                            
mongoose.connect(process.env.MONGODB_URL, {                            
    useNewUrlParser: true,                            
    useUnifiedTopology: true,                          
});                          

const db = mongoose.connection;                          
db.on("error", console.error.bind(console, "connection error:"));                          
db.once("open", () =&amp;gt; console.log("Connected to database"));            

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.onroad", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe(async (msg, ack, nack) =&amp;gt; {                
        const onroadParcel = await Track.updateOne(                  
            { name: msg.name },                  
            { status: msg.status },                  
            (err, parcel) =&amp;gt; {                    
                if (err) throw err;                    
                else return parcel;                  
            }                
        );                
        console.log("parcel is on road:", onroadParcel);                
        ack();              
    });    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//deliveredConsumer.js

import Tortoise from "tortoise";              
import mongoose from "mongoose"              
import Track from "../model/Tracking";              
import dotenv from "dotenv"              
dotenv.config()                            
mongoose.connect(process.env.MONGODB_URL, {                            
    useNewUrlParser: true,                            
    useUnifiedTopology: true,                          
});                          

const db = mongoose.connection;                          
db.on("error", console.error.bind(console, "connection error:"));                          
db.once("open", () =&amp;gt; console.log("Connected to database"));              

const tortoise = new Tortoise(process.env.AMQP_SERVER);            
tortoise              
    .queue("", { durable: false })              
    .exchange("parcel-tracking", "topic", "*.delivered", { durable: false })              
    .prefetch(1)              
    .json()              
    .subscribe(async (msg, ack, nack) =&amp;gt; {                
        const deliveredParcel = await Track.updateOne(                  
            { name: msg.name },                  
            { status: msg.status },                  
            (err, parcel) =&amp;gt; {                    
                if (err) throw err;                    
                else return parcel;                  
            }                
        );                
        console.log("parcel was delivered:", deliveredParcel);                
        ack();              
    });         
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these configurations have done, run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./server --exec babel-node -e js            
nodemon ./consumers/shippingConsumer --exec babel-node -e js            
nodemon ./consumers/onroadConsumer --exec babel-node -e js            
nodemon ./consumers/deliveredConsumer --exec babel-node -e js            
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see &lt;strong&gt;Connected to database&lt;/strong&gt; in all terminals, it means you skipped all steps successfully. Now open the links below up in a browser in order to test all database queries, and check the MongoDB database each time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/shipping/test"&gt;http://localhost:8000/shipping/test&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/onroad/test"&gt;http://localhost:8000/onroad/test&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/delivered/test"&gt;http://localhost:8000/delivered/test&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click the &lt;strong&gt;Collections&lt;/strong&gt; tab in the Cluster page in the MongoDB Cloud. In the page that came up, click the collection named &lt;strong&gt;tracks&lt;/strong&gt; under the &lt;strong&gt;parceltracking&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If our queries are correct, each time when shippingConsumer handles the message, a new record is going to be added to MongoDB, and each time other consumers handle the messages, the record will be updated. To show changes in the record, you will need to click the &lt;strong&gt;Refresh&lt;/strong&gt; button on the Collections page&lt;/p&gt;

&lt;p&gt;Now it is time to use Web Socket in order to render the changes in the records.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering Real-time Data With WebSocket
&lt;/h3&gt;

&lt;p&gt;To rendering real-time data with WebSocket, we are going to use the Socket.IO library. Each time consumers are making changes in the database, new records will be rendered on the front page of the application without refreshing the page. To do this let's create folder named &lt;strong&gt;socket&lt;/strong&gt; in the &lt;strong&gt;server&lt;/strong&gt; folder and add a file named &lt;strong&gt;trackerSocketç.js&lt;/strong&gt; in it. Later write these lines down in the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//trackerSocket.js

import socketIo from "socket.io";            
import express from "express";            
import http from "http";            
import mongoose from "mongoose";            
import Track from "../model/Tracking";            
import dotenv from "dotenv";            

dotenv.config();            

const port = process.env.WS_PORT || 8001;            

mongoose.connect(process.env.MONGODB_URL, {            
      useNewUrlParser: true,              
    useUnifiedTopology: true,            
});            

const db = mongoose.connection;            
db.on("error", console.error.bind(console, "connection error:"));            
db.once("open", () =&amp;gt; console.log("Connected to database"));            

const app = express();            

const server = http.createServer(app);            

const io = socketIo(server, {              
    cors: {                
    origin: "*",                
    methods: ["GET", "POST"],              
    },            
});            

let interval;            

const findParcel = async (socket) =&amp;gt; {              
    const parcel = await Track.find({}, (err, parcel) =&amp;gt; {                
        if (err) throw err;                
            console.log(parcel);                
            return parcel;              
    });              
    socket.emit("parcel", parcel);            
};            

io.on("connection", (socket) =&amp;gt; {              
    console.log("New client connected");              
    if (interval) {                
        clearInterval(interval);              
    }              
    interval = setInterval(() =&amp;gt; findParcel(socket), 1000);              
    socket.on("disconnect", () =&amp;gt; {                
        console.log("Client disconnected");                
        clearInterval(interval);                
    });                
});                

server.listen(port, () =&amp;gt; console.log(`Listening on port ${port}`));       
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After writing the lines, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon ./socket/trackerSocket --exec babel-node -e js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the messages &lt;strong&gt;Listening on port 8001&lt;/strong&gt; and &lt;strong&gt;Connected to database&lt;/strong&gt; output of the command, it means the command is running successfully.&lt;/p&gt;

&lt;p&gt;Now in order to frontend installation, open up a terminal, go to under the folder named &lt;strong&gt;parcel-tracking-system&lt;/strong&gt; where we created at the very first beginning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app client 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later on, under the &lt;strong&gt;client&lt;/strong&gt; folder, run this command in order to install socket.io-client library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add socket.io-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation process has ended, go to under the &lt;strong&gt;src&lt;/strong&gt; folder where is in the &lt;strong&gt;client&lt;/strong&gt; folder. Here, create a file named &lt;strong&gt;socket.js&lt;/strong&gt; and write these lines down in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//socket.js

import socketIOClient from "socket.io-client";            
const ENDPOINT = "http://127.0.0.1:8001";            

const socket = socketIOClient(ENDPOINT);            

export default socket;     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating &lt;strong&gt;sokcet.js&lt;/strong&gt;, open &lt;strong&gt;App.js&lt;/strong&gt; in the same directory and modify it like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.js

import React from "react";            
import socket from "./socket";            

function App() {              
    const [parcels, setParcel] = React.useState([{}]);              
    React.useEffect(() =&amp;gt; {                
        socket.on("parcel", (data) =&amp;gt; setParcel(data));                
    });                
    return (                  
        &amp;lt;div&amp;gt;                  
            {parcels.map((parcel) =&amp;gt; (                    
            &amp;lt;&amp;gt;                    
                &amp;lt;div&amp;gt;ID: {parcel._id}&amp;lt;/div&amp;gt;                    
                &amp;lt;div&amp;gt;Name: {parcel.name}&amp;lt;/div&amp;gt;                    
                &amp;lt;div&amp;gt;Status: {parcel.status}&amp;lt;/div&amp;gt;                    
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;                    
            &amp;lt;/&amp;gt;                    
        ))}                    
        &amp;lt;/div&amp;gt;                    
    );                    
}            

export default App;            
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run React application run this command under the &lt;strong&gt;client&lt;/strong&gt; directory in a terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the page automatically opened up, you should see the record in MongoDB. To add a new record ve update it, open up these links below respectively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/shipping/mycargo"&gt;http://localhost:8000/shipping/mycargo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/onroad/mycargo"&gt;http://localhost:8000/onroad/mycargo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://localhost:8000/delivered/mycargo"&gt;http://localhost:8000/delivered/mycargo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can see the changes in MongoDB with every click, congratulations, you have a little service!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Microservice structures are becoming more and more preferred structures in new applications. Software teams are working to migrate existing monolith structures to microservice architectures. In addition, AMQP stands out as a highly preferred communication protocol and provides solutions to some problems experienced with HTTP. On the end-user side, the interest and demand for real-time applications are also increasing and here web sockets provide great convenience to the software developer. It is a pleasure to master these tools, which are used to respond to new demands and new technologies, and it contributes a lot to the developer. Throughout this article, I wrote a small service and tried to provide a general understanding by using these structures and technologies in summary. I hope it has contributed to those who read, follow, and apply.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
