API Reference

amqplib

AMQP 0-9-1 client for Node.js to publish and consume messages with RabbitMQ queues and exchanges.

Official Documentation ↗
CompatibilityThis documentation covers version v2.2.1. Check the official docs for the latest API surface.

Installation

npmnpm install amqplib
pnpmpnpm add amqplib
yarnyarn add amqplib

Methods

Overview

amqplib speaks AMQP 0-9-1 to RabbitMQ, letting Node applications publish and consume messages through exchanges, queues and routing keys. It supports acknowledgements, confirms and dead-lettering.

Core Methods

Connect, then create a channel for most operations. assertQueue and assertExchange declare topology, sendToQueue or publish produce messages, and consume streams deliveries that you ack or nack.

Usage Notes

Declare durable topology, enable publisher confirms for reliability, and acknowledge only after processing succeeds. Route poison messages to a dead-letter queue instead of requeuing forever.

Common Pitfalls

Forgetting to ack leaves messages unacked and blocks the queue once the prefetch limit is reached. A channel-level error closes the whole channel, so recreate it. Auto-requeue on every failure can create an infinite redelivery loop.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/amqp-node/amqplib@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:08.078Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/amqp-node/amqplib@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:08.078Z.

FAQ

What is the difference between ack and nack?
ack tells the broker the message was handled and removes it. nack rejects it, optionally requeuing or routing it to a dead-letter exchange for later inspection.
How do I make messages survive a restart?
Use a durable queue, persistent messages and confirm channels. Without publisher confirms you cannot know the broker stored the message before the connection dropped.
Why does a channel close with an error?
A protocol error such as publishing to a missing exchange closes the channel. Recreate the channel, declare topology idempotently, and log the close reason to find the cause.