<?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>Forem: Kazi Naimul Hoque</title>
    <description>The latest articles on Forem by Kazi Naimul Hoque (@kazinaimul).</description>
    <link>https://forem.com/kazinaimul</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%2F920543%2Fc1f378a0-022f-420c-8220-1911ff46059c.png</url>
      <title>Forem: Kazi Naimul Hoque</title>
      <link>https://forem.com/kazinaimul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kazinaimul"/>
    <language>en</language>
    <item>
      <title>Simplify RabbitMQ Integration in Node.js with @kazinaimul/rabbitmq</title>
      <dc:creator>Kazi Naimul Hoque</dc:creator>
      <pubDate>Sat, 23 Dec 2023 04:58:50 +0000</pubDate>
      <link>https://forem.com/kazinaimul/rabbitmq-helper-package-simplifying-rabbitmq-integration-in-nodejs-1mh1</link>
      <guid>https://forem.com/kazinaimul/rabbitmq-helper-package-simplifying-rabbitmq-integration-in-nodejs-1mh1</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepg7qgky63qez47wx5bn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepg7qgky63qez47wx5bn.png" alt="Image description" width="288" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Are you tired of navigating the complexities of RabbitMQ integration in your Node.js projects? Well, fret no more! I’m excited to introduce the &lt;a href="https://www.npmjs.com/package/@kazinaimul/rabbitmq"&gt;@kazinaimul/rabbitmq&lt;/a&gt; package — a straightforward solution to streamline your RabbitMQ integration seamlessly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation Made Easy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s kick things off with a swift installation. Open your terminal and run:&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 @kazinaimul/rabbitmq

or

npm install @kazinaimul/rabbitmq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Getting Started: Establishing Connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In your Express app’s &lt;code&gt;app.js&lt;/code&gt; file, pave the way for RabbitMQ integration with this simple code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { RabbitMQConnection } from '@kazinaimul/rabbitmq';
RabbitMQConnection.connect('RABBITMQ_CONNECTION_URL');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swap out &lt;code&gt;RABBITMQ_CONNECTION_URL&lt;/code&gt; with your RabbitMQ server connection URL, neatly formatted as &lt;code&gt;_**amqp://username:password@localhost:5672**_&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effortless Message Publishing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Publishing messages becomes a breeze with the creation of a &lt;code&gt;Publisher&lt;/code&gt; class. Check out this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Publisher } from '@kazinaimul/rabbitmq';
export class PublishMessage extends Publisher {
 constructor() {
   const queueName = 'queue-name';
   super(queueName);
 }
async publish&amp;lt;MessageType&amp;gt;(message: MessageType): Promise&amp;lt;void&amp;gt; {
   try {
     const customOptions = {
     exchange: `Exchange_name`,
     routingKey: queue,
     delay: 0,
     exchangeType: "direct",
     headers: {},
     }; // Custom optional values, overwriting default options
     await this.rabbitMQClient.publish(this.queueName, message, customOptions);
   } catch (error) {
     console.error('Error publishing messages:', error);
   }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default the option for publish a message is followed which can be replaced:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const defaultOptions = {&lt;br&gt;
    exchange:&lt;/code&gt;Exchange_${queue}&lt;code&gt;,&lt;br&gt;
    routingKey: queue,&lt;br&gt;
    delay: 0,&lt;br&gt;
    exchangeType: "direct",&lt;br&gt;
    headers: {},&lt;br&gt;
};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Seamless Message Consumption&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Creating a &lt;code&gt;Consumer&lt;/code&gt; class ensures smooth message consumption. Take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Consumer } from '@kazinaimul/rabbitmq';
export class MyConsumer extends Consumer {
 constructor() {
   const queueName = 'queue-name';
   const options = {
     retry: true, // If true, messages will be queued again in case of an error (default is true)
     retry_count: 3, // Maximum retry count, beyond which the message will be moved to an error queue
     retry_delay: 0 // Delay in milliseconds for retries
   };
   super(queueName, options); // Options are an optional field
 }
async execute&amp;lt;T extends object&amp;gt;(message: T): Promise&amp;lt;void&amp;gt; {
 // Implement your own logic to handle the consumed message
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Register and Consume&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don’t forget to register your consumers in a file (let’s call it &lt;code&gt;consumerRegister.ts&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import the consumer classes
import { MyConsumer } from './MyConsumer';
export const consumerRegister: any[] = [
  new MyConsumer()
  // Push all other Consumer classees here
];
In your application, consume messages effortlessly by iterating through the registered consumers:

import { consumerRegister } from './consumerRegister';
// Consume messages from registered consumers
for (const consumer of consumerRegister) {
 consumer.consume();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;’queue-name’&lt;/code&gt; with your chosen queue name.&lt;/p&gt;

&lt;p&gt;Feel free to explore, contribute, and simplify your RabbitMQ integration. Happy coding! 🚀🐇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/@kazinaimul/rabbitmq"&gt;Check out the package on npm!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>rabbitmq</category>
      <category>npm</category>
    </item>
    <item>
      <title>Boilerplate for Typescript-Express with sequelize ORM</title>
      <dc:creator>Kazi Naimul Hoque</dc:creator>
      <pubDate>Tue, 08 Nov 2022 08:19:16 +0000</pubDate>
      <link>https://forem.com/kazinaimul/boilerplate-for-typescript-express-with-sequelize-orm-3eeg</link>
      <guid>https://forem.com/kazinaimul/boilerplate-for-typescript-express-with-sequelize-orm-3eeg</guid>
      <description>&lt;h1&gt;
  
  
  &lt;a href="https://github.com/kazi-naimul/typescript-express-mysql-boilerplate"&gt;Boilerplate for Typescript-Express with sequelize ORM&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;A boilerplate for any enterprise rest api or service with Node.js -Typescript, Express and Sequelize ORM for mysql, postgresql or others.&lt;/p&gt;

&lt;p&gt;By running this project you will get a production ready environment with all necessary supports for validation, unit testing, socket, redis and many more.This repo is the typescript version of my another boilerplate of &lt;a href="https://github.com/kazi-naimul/node-express-mysql-boilerplate"&gt;Nodejs Express Mysql boilerplate&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Manual Installation
&lt;/h2&gt;

&lt;p&gt;Clone the repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/kazi-naimul/typescript-express-mysql-boilerplate
&lt;span class="nb"&gt;cd &lt;/span&gt;typescript-express-mysql-boilerplate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the environment variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env

&lt;span class="c"&gt;# open .env and modify the environment variables (if needed)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ORM&lt;/strong&gt;: &lt;a href="https://sequelize.org/"&gt;Sequelize&lt;/a&gt;  orm for object data modeling&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Migration and Seed&lt;/strong&gt;: DB migration and Seed using &lt;a href="https://github.com/sequelize/cli"&gt;Sequelize-CLI&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication and authorization&lt;/strong&gt;: using &lt;a href="http://www.passportjs.org"&gt;passport&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error handling&lt;/strong&gt;: centralized error handling&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation&lt;/strong&gt;: request data validation using &lt;a href="https://github.com/hapijs/joi"&gt;Joi&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging&lt;/strong&gt;: using &lt;a href="https://github.com/winstonjs/winston"&gt;winston&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: unittests using &lt;a href="https://mochajs.org/"&gt;Mocha&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: Caching using &lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bidirectional Communication&lt;/strong&gt;: using &lt;a href="https://socket.io/"&gt;Scoket&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Job scheduler&lt;/strong&gt;: with &lt;a href="https://www.npmjs.com/package/node-cron"&gt;Node-cron&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency management&lt;/strong&gt;: with &lt;a href="https://yarnpkg.com"&gt;Yarn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment variables&lt;/strong&gt;: using &lt;a href="https://github.com/motdotla/dotenv"&gt;dotenv&lt;/a&gt; and &lt;a href="https://github.com/kentcdodds/cross-env#readme"&gt;cross-env&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CORS&lt;/strong&gt;: Cross-Origin Resource-Sharing enabled using &lt;a href="https://github.com/expressjs/cors"&gt;cors&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Docker support&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linting&lt;/strong&gt;: with &lt;a href="https://eslint.org"&gt;ESLint&lt;/a&gt; and &lt;a href="https://prettier.io"&gt;Prettier&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Commands
&lt;/h2&gt;

&lt;p&gt;Running locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running in production:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Testing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# run all tests&lt;/span&gt;
yarn &lt;span class="nb"&gt;test&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Environment Variables
&lt;/h2&gt;

&lt;p&gt;The environment variables can be found and modified in the &lt;code&gt;.env&lt;/code&gt; file. They come with these default values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Server environment&lt;/span&gt;
&lt;span class="nv"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;development
&lt;span class="c"&gt;#Port number&lt;/span&gt;
&lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5000

&lt;span class="c"&gt;#Db configuration&lt;/span&gt;
&lt;span class="nv"&gt;DB_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;db-host
&lt;span class="nv"&gt;DB_USER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;db-user
&lt;span class="nv"&gt;DB_PASS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;db-pass
&lt;span class="nv"&gt;DB_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;db-name


&lt;span class="c"&gt;# JWT secret key&lt;/span&gt;
&lt;span class="nv"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-jwt-secret-key
&lt;span class="c"&gt;# Number of minutes after which an access token expires&lt;/span&gt;
&lt;span class="nv"&gt;JWT_ACCESS_EXPIRATION_MINUTES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5
&lt;span class="c"&gt;# Number of days after which a refresh token expires&lt;/span&gt;
&lt;span class="nv"&gt;JWT_REFRESH_EXPIRATION_DAYS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;30

&lt;span class="c"&gt;#Log config&lt;/span&gt;
&lt;span class="nv"&gt;LOG_FOLDER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;logs/
&lt;span class="nv"&gt;LOG_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;%DATE%-app-log.log
&lt;span class="nv"&gt;LOG_LEVEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;error

&lt;span class="c"&gt;#Redis&lt;/span&gt;
&lt;span class="nv"&gt;REDIS_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;redis-host
&lt;span class="nv"&gt;REDIS_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;6379
&lt;span class="nv"&gt;REDIS_USE_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;no
&lt;span class="nv"&gt;REDIS_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-password

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;specs\
src\
 |--config\         # Environment variables and configuration related things
 |--controllers\    # Route controllers (controller layer)
 |--dao\            # Data Access Object for models
    |--contract\        # Contracts for all dao
    |--implementation   # Implementation of the contracts
 |--db\             # Migrations and Seed files
 |--models\         # Sequelize models (data layer)
 |--routes\         # Routes
 |--services\       # Business logic (service layer)
    |--contract\        # Contracts for all service
    |--implementation   # Implementation of the contracts
 |--helper\         # Helper classes and functions
 |--validations\    # Request data validation schemas
 |--app.js          # Express app
 |--cronJobs.ts     # Job Scheduler
 |--index.ts        # App entry point
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Boilerplate for Node-Express with sequelize ORM</title>
      <dc:creator>Kazi Naimul Hoque</dc:creator>
      <pubDate>Sun, 04 Sep 2022 07:12:39 +0000</pubDate>
      <link>https://forem.com/kazinaimul/boilerplate-for-node-express-with-sequelize-orm-a6n</link>
      <guid>https://forem.com/kazinaimul/boilerplate-for-node-express-with-sequelize-orm-a6n</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/kazi-naimul/node-express-mysql-boilerplate"&gt;https://github.com/kazi-naimul/node-express-mysql-boilerplate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A boilerplate for any enterprise rest api or service with Node.js, Express and Sequelize ORM for mysql, postgresql or others.&lt;/p&gt;

&lt;p&gt;By running this project you will get a production ready environment with all necessary supports for validation, unit testing, socket, redis and many more.&lt;/p&gt;

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