Created
March 26, 2022 13:54
-
-
Save hoangsetup/354e42a6fd9d3992489b716f0ba83b92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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