Skip to content

Instantly share code, notes, and snippets.

@hoangsetup
Created March 26, 2022 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoangsetup/354e42a6fd9d3992489b716f0ba83b92 to your computer and use it in GitHub Desktop.
Save hoangsetup/354e42a6fd9d3992489b716f0ba83b92 to your computer and use it in GitHub Desktop.
import {
Channel,
connect,
Connection,
} from 'amqplib';
import environments from './environments';
let connection: Connection;
const channels: Record<string, Channel> = {};
export default async function getChannel(queue: string): Promise<Channel> {
if (channels[queue]) {
return channels[queue];
}
if (!connection) {
connection = await connect({
hostname: environments.rabbitMQHostname,
port: environments.rabbitMQPort,
username: environments.rabbitMQUsername,
password: environments.rabbitMQPassword,
});
}
channels[queue] = await connection.createChannel();
await channels[queue].assertQueue(
queue,
{
durable: true,
autoDelete: false,
},
);
await channels[queue].prefetch(1);
return channels[queue];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment