1 year ago

#341850

test-img

BokuNoHeeeero

How to reuse connection to amqp across the whole app?

I have a problem with reusing of the connection to amqp in my express/typescript app. Below is my code:

import * as amqp from "amqplib";

export const amqpConnection = async (): Promise<amqp.Channel> => {
    const connection = await amqp.connect("amqp://localhost");
    const channel = await connection.createChannel();

    return channel;
}

export const sendMessageToQueue = async (message: any) => {
    const channel = await amqpConnection();
    const msg = channel.sendToQueue("queue_name", Buffer.from(JSON.stringify(message)));
    console.log("sendMessageToQueue: ", message.id);
}

Right now I'm using function amqpConnection in which I create new channel and return this channel, connection is created on every time I launch sendMessageToQueue function, and I don't want it.

How to create reusable using of the connection or channel across the whole my express app?

Thanks for any help!

node.js

typescript

express

rabbitmq

amqp

0 Answers

Your Answer

Accepted video resources