<?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: Abhishek Yadav</title>
    <description>The latest articles on DEV Community by Abhishek Yadav (@aiabhishek).</description>
    <link>https://dev.to/aiabhishek</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%2F1088981%2F5f147252-89f6-44e0-aa49-e8964eff7002.png</url>
      <title>DEV Community: Abhishek Yadav</title>
      <link>https://dev.to/aiabhishek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aiabhishek"/>
    <language>en</language>
    <item>
      <title>Kubernetes and Helm Package Manager</title>
      <dc:creator>Abhishek Yadav</dc:creator>
      <pubDate>Sat, 07 Dec 2024 17:51:39 +0000</pubDate>
      <link>https://dev.to/aiabhishek/kubernetes-and-helm-package-manager-19el</link>
      <guid>https://dev.to/aiabhishek/kubernetes-and-helm-package-manager-19el</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Kubernetes has become the de facto standard for container orchestration, enabling developers to deploy, scale, and manage containerized applications efficiently. However, managing Kubernetes manifests (YAML files) for complex applications can quickly become overwhelming. That’s where Helm, the package manager for Kubernetes, steps in to simplify the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Kubernetes?
&lt;/h2&gt;

&lt;p&gt;Kubernetes is a powerful open-source platform that automates the deployment, scaling, and management of containerized applications.&lt;br&gt;
The main components of Kubernetes are &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Master Node Components&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;API Server: Acts as the gateway for all Kubernetes API requests. It validates and processes REST commands.&lt;/li&gt;
&lt;li&gt;Controller Manager: Manages controllers (e.g., Node Controller, Deployment Controller) that ensure the desired state of cluster objects.&lt;/li&gt;
&lt;li&gt;Scheduler: Assigns pods to nodes based on resource availability and scheduling policies.&lt;/li&gt;
&lt;li&gt;etcd: A key-value store that holds the cluster's state and configuration data.&lt;/li&gt;
&lt;li&gt;Worker Node Components&lt;/li&gt;
&lt;li&gt;Kubelet: Ensures that containers are running in pods.&lt;/li&gt;
&lt;li&gt;Kube-proxy: Handles networking, including forwarding requests to appropriate pods.&lt;/li&gt;
&lt;li&gt;Container Runtime: Manages container lifecycle (e.g., Docker, containerd).&lt;/li&gt;
&lt;li&gt;Workload Objects&lt;/li&gt;
&lt;li&gt;Pod: The smallest deployable unit; encapsulates containers and shared resources like storage and networking.&lt;/li&gt;
&lt;li&gt;Service: Exposes pods to the network, ensuring stable connectivity.&lt;/li&gt;
&lt;li&gt;Deployment: Manages the desired state of pods, supports scaling and rolling updates.&lt;/li&gt;
&lt;li&gt;ConfigMap &amp;amp; Secret: Provide externalized configurations and secure data, respectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What is Helm?
&lt;/h2&gt;

&lt;p&gt;Helm is often called the "Yum or Apt of Kubernetes." It simplifies Kubernetes application deployment by bundling all the manifests into a single reusable package called a chart. With Helm, you can Package all resources (Deployments, Services, ConfigMaps, etc.) into a chart.&lt;br&gt;
Version-control your deployments.&lt;br&gt;
Customize application configurations through values files.&lt;br&gt;
Roll back to previous deployments in case of failure.&lt;/p&gt;

&lt;p&gt;Why Use Helm?&lt;br&gt;
Here’s why Helm is a game-changer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simplified Deployment&lt;br&gt;
Instead of managing individual YAML files, you use Helm charts that &lt;br&gt;
organize all resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reusability&lt;br&gt;
Charts can be reused across environments (development, staging, &lt;br&gt;
production) by overriding values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ease of Updates&lt;br&gt;
Helm makes rolling updates and rollbacks straightforward.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consistency&lt;br&gt;
Standardizes application deployments across teams.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Getting Started with Helm
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install&lt;br&gt;
Install helm &lt;a href="https://helm.sh/docs/intro/install/" rel="noopener noreferrer"&gt;https://helm.sh/docs/intro/install/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Helm Chart&lt;br&gt;
Use the helm create command to scaffold a new chart:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;helm create my-app-deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lets say we want to deploy three services frontend, backend and Redis.&lt;br&gt;
Now we will customise helm chart to deploy these services.&lt;/p&gt;

&lt;p&gt;First edit values.yaml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;replicaCount: 1

backend:
  image: sample-backend
  service:
    type: ClusterIP
    port: 8000
  env:
    REDIS_URI: redis://redis-svc:6379

frontend:
  image: sample-frontend
  service:
    type: NodePort
    port: 5173
    nodePort: 31000
  env:
    BACKEND_URL: http://backend-svc:8000

redis:
  image: redis
  service:
    type: ClusterIP
    port: 6379

namespace: default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Update Chart.yaml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
name: my-app-deploy
description: A Helm chart for deploying backend, frontend, and redis
version: 1.0.0
appVersion: "1.0.0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside your templates folder update service.yaml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
apiVersion: v1
kind: Service
metadata:
  name: backend-svc
  namespace: {{ .Values.namespace }}
spec:
  type: {{ .Values.backend.service.type }}
  selector:
    app: backend
  ports:
  - protocol: TCP
    port: {{ .Values.backend.service.port }}
    targetPort: {{ .Values.backend.service.port }}

---
apiVersion: v1
kind: Service
metadata:
  name: frontend-svc
  namespace: {{ .Values.namespace }}
spec:
  type: {{ .Values.frontend.service.type }}
  selector:
    app: frontend
  ports:
  - protocol: TCP
    port: {{ .Values.frontend.service.port }}
    targetPort: {{ .Values.frontend.service.port }}
    nodePort: {{ .Values.frontend.service.nodePort }}

---
apiVersion: v1
kind: Service
metadata:
  name: redis-svc
  namespace: {{ .Values.namespace }}
spec:
  type: {{ .Values.redis.service.type }}
  selector:
    app: redis
  ports:
  - protocol: TCP
    port: {{ .Values.redis.service.port }}
    targetPort: {{ .Values.redis.service.port }}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally update deployment.yaml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-backend
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: backend
        image: {{ .Values.backend.image }}
        ports:
        - containerPort: {{ .Values.backend.service.port }}
        env:
        - name: REDIS_URI
          value: {{ .Values.backend.env.REDIS_URI }}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-frontend
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: {{ .Values.frontend.image }}
        ports:
        - containerPort: {{ .Values.frontend.service.port }}
        env:
        - name: BACKEND_URL
          value: {{ .Values.frontend.env.BACKEND_URL }}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-redis
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: {{ .Values.redis.image }}
        ports:
        - containerPort: {{ .Values.redis.service.port }}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before going further make sure you installed minikube and and it is running properly.&lt;br&gt;
Complete installation guide &lt;a href="https://minikube.sigs.k8s.io/docs/start/" rel="noopener noreferrer"&gt;https://minikube.sigs.k8s.io/docs/start/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's focus on helm&lt;/p&gt;

&lt;p&gt;Deploy the Helm Chart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;helm install my-app-deploy ./my-app-deploy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify deployment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get deployments
kubectl get services

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out this repo for reference &lt;a href="https://github.com/AIABHISHEK/cosmocloud-deploy" rel="noopener noreferrer"&gt;https://github.com/AIABHISHEK/cosmocloud-deploy&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
      <category>docker</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Streamline Your Smart Contract Development with Foundry</title>
      <dc:creator>Abhishek Yadav</dc:creator>
      <pubDate>Thu, 07 Nov 2024 16:23:25 +0000</pubDate>
      <link>https://dev.to/aiabhishek/streamline-your-smart-contract-development-with-foundry-1c40</link>
      <guid>https://dev.to/aiabhishek/streamline-your-smart-contract-development-with-foundry-1c40</guid>
      <description>&lt;h2&gt;
  
  
  What is foundry??
&lt;/h2&gt;

&lt;p&gt;Foundry is a comprehensive toolchain specifically designed to empower developers in the realm of smart contract creation. It streamlines the entire development process, offering a robust set of features that manage dependencies, compile code, run tests, facilitate deployments, and even enable interaction with the blockchain through both command-line and Solidity scripts. In essence, Foundry acts as a one-stop shop for smart contract development, eliminating the need to juggle multiple disparate tools.&lt;br&gt;
More on &lt;a href="https://book.getfoundry.sh/" rel="noopener noreferrer"&gt;official website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To summarize we can it does everything we require in the blockchain development and comes built in with utility tools.&lt;/p&gt;

&lt;p&gt;How to install??&lt;br&gt;
Before going further make sure you have installed &lt;a href="https://gitforwindows.org/" rel="noopener noreferrer"&gt;GIT BASH&lt;/a&gt; because these commands will not work on windows terminal.&lt;br&gt;
Before installing foundry we need to install latest version of the rust.&lt;br&gt;
To install rust run below command in your bash terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check rust is correctly installed 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;cargo --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install foundry run this command in your bash terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -L https://foundry.paradigm.xyz | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install following tools&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Forge&lt;/li&gt;
&lt;li&gt;Cast&lt;/li&gt;
&lt;li&gt;Anvil&lt;/li&gt;
&lt;li&gt;Chisel&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to learn in depth about these tools check out this &lt;a href="https://book.getfoundry.sh/" rel="noopener noreferrer"&gt;official doc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's start our project using foundry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ forge init &amp;lt;project_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create template project in in this directory &lt;br&gt;
cd into the folder.&lt;br&gt;
Now let's understand all the folder one by one.&lt;br&gt;
src --&amp;gt; In this folder we will write all our smart contracts &lt;br&gt;
script --&amp;gt; here we write our deploy scripts&lt;br&gt;
For Now these to enough to write our smart contract and continue working.&lt;br&gt;
Now Delete  existing file from the src directory and create a new file and write your smart contract.&lt;/p&gt;

&lt;p&gt;For Example: let's create a file with name &lt;strong&gt;myContract.sol&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;//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract myContract {
    uint256 myNumber;

    function store(uint256 _myNumber) public {
        myNumber = _myNumber;
    }

    function getMyNumber() public view returns (uint256) {
        return myNumber;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Delete existing file in file script directory and add your file with name DeploymentScript.s.sol&lt;br&gt;
Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.18;

import { myContract } from "../src/SimpleStore.sol";
import  "../lib/forge-std/src/Script.sol";

contract DeploymentScript is Script {
    function run() external returns (myContract) {

        // all transactions will take place in between the startBroadcast() and stopBroadcast()
        vm.startBroadcast();
        myContract myc = new myContract();
        vm.stopBroadcast();
        return myc;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code are importing our contract then importing standard lib provided in lib folder&lt;/p&gt;

&lt;p&gt;Since we have created our contract let's compile it make sure everything is ok&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets deploy our contract although you can deploy your contract using deploy script but we can also use our bash terminal to deploy contract.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to deploy
&lt;/h3&gt;

&lt;p&gt;First 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;anvil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run local blockchain node&lt;/p&gt;

&lt;p&gt;Now run below command to deploy our smart contract&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge create &amp;lt;your_contract_name&amp;gt; --interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default if you will not provide rpc-url it will try to deploy on local node where anvil running.&lt;br&gt;
If you are using Ganache and need to provide an RPC URL, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge create &amp;lt;your_contract_name&amp;gt; --rpc-url &amp;lt;your_rpc-url&amp;gt; --interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's use our deploy script to deploy our smart contract&lt;br&gt;
Use 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;forge script script/DeploymentScript.s.sol --rpc-url &amp;lt;rpc-url&amp;gt; --interactive --sender &amp;lt;address&amp;gt; --broadcast -vvvv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to copy address and private key corresponding to that address from bash terminal where anvil running&lt;/p&gt;

&lt;p&gt;Once you run above command you will see some in your anvil bash terminal.&lt;/p&gt;

&lt;p&gt;We use cast to interact with deployed smart contract from bash terminal.&lt;/p&gt;

&lt;p&gt;To call &lt;em&gt;store&lt;/em&gt; function we can use following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast send 0x5FbDB2315678afecb367f032d93F642f64180aa3 "store(uint256)" 123 --rpc-url http://127.0.0.1:8545 --interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when prompted give private key of wallet (you can get it from bash terminal of anvil)&lt;/p&gt;

&lt;p&gt;Now to call getMyNumber which is view function we can use following  command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast call 0x5FbDB2315678afecb367f032d93F642f64180aa3 "getMyNumber()" --rpc-url http://127.0.0.1:8545
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may have noticed when call we store function we do  need private key where as while we call &lt;em&gt;getMyNumber&lt;/em&gt; we do not.&lt;br&gt;
Let's understand the reason:&lt;br&gt;
In Solidity, functions can be categorized into two main types: state-changing functions and view/pure functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State-changing functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These functions can modify the state of the blockchain. Any modification to the contract's state variables or storage will require a transaction, and transactions in Ethereum incur gas fees.&lt;br&gt;
Examples include functions that modify the contract's state, such as writing to storage variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;View and Pure functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These functions are read-only and do not modify the state. They don't require a transaction and can be called without incurring gas fees. View functions are allowed to read the state of the contract but cannot modify it. Pure functions do not read or modify the state; they are entirely self-contained.&lt;/p&gt;

&lt;p&gt;The store function modifies the state by updating the &lt;em&gt;myNumber&lt;/em&gt; state variable. Therefore, it is a state-changing function and requires a transaction, incurring gas fees.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;getMyNumber&lt;/em&gt; function, on the other hand, is declared as view, indicating that it only reads from the state and does not modify it. As a result, it can be called without a transaction and does not incur gas fees.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>smartcontract</category>
      <category>solidity</category>
      <category>foundry</category>
    </item>
    <item>
      <title>How to Dockerize Your Node.js App in Minutes</title>
      <dc:creator>Abhishek Yadav</dc:creator>
      <pubDate>Sat, 16 Mar 2024 17:49:46 +0000</pubDate>
      <link>https://dev.to/aiabhishek/how-to-dockerize-your-nodejs-app-in-minutes-4nn3</link>
      <guid>https://dev.to/aiabhishek/how-to-dockerize-your-nodejs-app-in-minutes-4nn3</guid>
      <description>&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin, make sure you have Docker installed on your development machine. You can download and install Docker Desktop from the official Docker website &lt;a href="https://docs.docker.com/desktop/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for your operating system. &lt;/p&gt;

&lt;p&gt;Once Docker is installed, ensure that you have a working Node.js application that you want to dockerize.&lt;br&gt;
You can clone this repo to get started this is simple node-app &lt;a href="https://github.com/AIABHISHEK/dockerizing-node-app" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting Up Dockerfile
&lt;/h2&gt;

&lt;p&gt;The first step in dockerizing your Node.js application is creating a Dockerfile. This file contains instructions for building a Docker image that encapsulates your application and its dependencies. &lt;br&gt;
Create a dockerfile in your node-app directory.&lt;/p&gt;

&lt;p&gt;Here's a simple Dockerfile to get you started:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official Node.js image based on Alpine Linux with the latest LTS version
FROM node:alpine

# Set the working directory inside the container
WORKDIR .

# Copy package.json and package-lock.json to the working directory
COPY package.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the application
CMD ["node", "app.js"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building Your Docker Image
&lt;/h2&gt;

&lt;p&gt;Once you've created your Dockerfile, it's time to build your Docker image. Open a terminal or command prompt, navigate to the directory containing your Node.js application and Dockerfile, and run the following command&lt;br&gt;
You can specify your image name using the -t flag.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t your-image-name .&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Your Docker Container
&lt;/h2&gt;

&lt;p&gt;With your Docker image built, you can now run a Docker container based on that image. Use the following command&lt;br&gt;
&lt;code&gt;docker run -p 4000:5000 your_image_name&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This command will start a container from your image and map port 5000 of the container to port 4000 on your host machine, allowing you to access the application via&lt;br&gt;
&lt;a href="http://localhost:4000" rel="noopener noreferrer"&gt;http://localhost:4000&lt;/a&gt;&lt;br&gt;
Regardless of the EXPOSE settings, you can override them at runtime by using the -p as we are doing in the command.&lt;/p&gt;

&lt;p&gt;Congratulations! You've successfully dockerized your Node.js application.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>docker</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Connect Express app to RabbitMQ</title>
      <dc:creator>Abhishek Yadav</dc:creator>
      <pubDate>Tue, 26 Sep 2023 15:38:36 +0000</pubDate>
      <link>https://dev.to/aiabhishek/how-to-connect-express-app-to-rabbitmq-4gjo</link>
      <guid>https://dev.to/aiabhishek/how-to-connect-express-app-to-rabbitmq-4gjo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;RabbitMQ is an open-source message broker software that facilitates message queuing. It provides a platform for different software components to communicate by sending and receiving messages.&lt;br&gt;
Now let's go through the process of connecting RabbitMQ to our Express App&lt;br&gt;
&lt;strong&gt;Step-1&lt;/strong&gt;&lt;br&gt;
Run RabbitMQ on your machine&lt;br&gt;
Easiest way to do this use docker image of rabbitmq and run this docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker pull rabbitmq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the docker image using this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 15672:15672 &lt;span class="nt"&gt;-p&lt;/span&gt; 5672:5672 &lt;span class="nt"&gt;--hostname&lt;/span&gt; my-rabbitmq &lt;span class="nt"&gt;--name&lt;/span&gt; my-rabbitmq-container rabbitmq:3-management
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2&lt;/strong&gt;&lt;br&gt;
Create Express app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/orders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Order submitted&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test your app using postman&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-3&lt;/strong&gt;&lt;br&gt;
Now it is time add code to connect to rabbitmq&lt;br&gt;
To rabbitmq we need amqplib npm package install this package in your project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install amqplib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add code for connection to rabbitmq in your express app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amqplib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;amqplib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9005&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amqpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;amqp://localhost:5672&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;amqplib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amqpServer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createChannel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/orders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Order placed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendToQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;})));&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Order submitted&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make sure your you are connected to rabbitmq you can check rabbitmq management dashboard&lt;br&gt;
You can access it on&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:15672/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;login credentials are&lt;br&gt;
Username : guest&lt;br&gt;
Password : guest&lt;br&gt;
Now check connection you will find you are connected &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb299s6rdgt0qv6raewqe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb299s6rdgt0qv6raewqe.png" alt="Connection tab image" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now check queue you will find queue created with the name you have given to your queue in my case order&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3d8g8krqwz5rg0hvcfpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3d8g8krqwz5rg0hvcfpz.png" alt="Queue tab image" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to check you are receiving the data you are sending to queue and perform operation on that make another project install amqplib&lt;br&gt;
Create a file with name worker.js&lt;/p&gt;

&lt;p&gt;Now write the code to connect to rabbitmq and also to receive message sent by our express app from the queue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amqp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;amqplib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectToRabbitMQ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;amqp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;amqp://localhost:5672&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createChannel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error connecting to RabbitMQ:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;receiveMessageFromQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queueName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectToRabbitMQ&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queueName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;durable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queueName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;//perform operations on the message&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Received message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queueName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Replace with your queue name&lt;/span&gt;
&lt;span class="nf"&gt;receiveMessageFromQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queueName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The receiveMessageFromQueue function will process the message sent to queue.&lt;/p&gt;

&lt;p&gt;Thank you for taking the time to read. I'm in a continuous process of learning and sharing. If you spot any mistakes in my understanding or have any other insights, please feel free to let me know in the comments section.&lt;/p&gt;

</description>
      <category>rabbitmq</category>
      <category>express</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
