Giter Site home page Giter Site logo

nodejs-rabbitmq-basic-project's Introduction

How to works that sh*t RabbitMQ

What is RabbitMQ

  • Basicly RabbitMQ is a queue manage system. For example There is a register form and You wanna verify email after customer registered it. Normaly when the customer clicked the button Mail will be work on background after clicking. it seems normal right? but If the there are 1000 customers and they clicked the button for registering by the same time, what would be that time? That's why We can use RabbitMQ for this solving. without keeping customer waiting it add queue and after that I delete the queue after the work is done, it deletes.

rabbitmqConnection.js

  • There is a connection rabbitMQ.
const amqp = require('amqplib')

module.exports = async () =>{
    const connection = await amqp.connect();
    return connection;
}

publisher.js

  • It sends a queue what you wanna send inside queue
const amqp = require('amqplib');
const rabbitMqConnection = require('./rabbitmqConnection');
const queueName = 'emailQueueName';

module.exports = async (email)=> {
    try {
    const connection = await rabbitMqConnection();
    const channel = await connection.createChannel();
    await channel.assertQueue(queueName);
    channel.sendToQueue(queueName, Buffer.from(JSON.stringify({email: email})))
    console.log('it went to queue!!!')

    }catch(error){
        console.error(`Something is wrong ${error}`)
    }
}

consume.js

  • Finally that consume to queue so It get whatever inside queue and we should delete the queue with act() function
const amqp = require('amqplib');
const rabbitMqConnection = require('./rabbitmqConnection');
const queueName = 'emailQueueName';


async function onConsumeEmail(){
    const connection = await rabbitMqConnection();
    const channel = await connection.createChannel();
    await channel.assertQueue(queueName);
    channel.consume(queueName, (email)=>{
        setTimeout(()=>{
            const mail = JSON.parse(email.content.toString());
            console.log(`Email went successfully ${mail.email}`);
            channel.ack(email);
        },10000)
    })
}

onConsumeEmail();

nodejs-rabbitmq-basic-project's People

Contributors

tuzlu07x avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.