AMQP Node.js Client
The recommended Node.js client is amqp-client.js. It has no dependencies, reconnects on its own, and recovers consumers after a reconnect. Install it with:
npm install @cloudamqp/amqp-client
Publish and consume
The example reads the broker URL from BROKER_URL and falls back to a local LavinMQ at amqp://guest:guest@localhost.
Save the connection helper as amqp.js:
import { AMQPSession } from "@cloudamqp/amqp-client"
// The session reconnects automatically and recovers consumers
export function connect() {
return AMQPSession.connect(process.env.BROKER_URL || "amqp://guest:guest@localhost")
}
Save the publisher as publisher.js:
import { connect } from "./amqp.js"
// Resolves once the broker has confirmed the message
export async function publish(session, queueName, message) {
const queue = await session.queue(queueName, { durable: true })
await queue.publish(message, { deliveryMode: 2 })
console.log(` [x] Sent '${message}'`)
}
if (import.meta.main) {
const session = await connect()
await publish(session, "hello", "Hello world!")
await session.stop()
}
Save the consumer as worker.js in the same directory:
import { connect } from "./amqp.js"
// The message is acked when the callback returns, and requeued if it throws
export async function consume(session, queueName, handler) {
const queue = await session.queue(queueName, { durable: true })
return queue.subscribe({ prefetch: 10 }, async (msg) => {
await handler(msg.bodyString() ?? "")
})
}
if (import.meta.main) {
const session = await connect()
await consume(session, "hello", (message) => console.log(` [x] Received '${message}'`))
console.log(" [*] Waiting for messages. To exit press CTRL+C")
process.on("SIGINT", async () => {
await session.stop()
})
}
The files are ES modules and need Node.js 24.2 or later. The package.json with "type": "module" is in the example repo. Run npm install, then node worker.js in one terminal and node publisher.js in another.
The full, runnable example lives in javascript/amqp-client in cloudamqp/examples, and CI tests it against LavinMQ.
Good to know
- Messages get acked after the callback returns. If the callback throws, the client nacks the message and LavinMQ requeues it. A message can arrive twice after a reconnect, so keep handlers idempotent.
- Set a prefetch limit.
subscribesets no prefetch by default, so LavinMQ pushes messages without limit. The example passessubscribe({ prefetch: 10 }, ...)to cap unacked messages per consumer. publishwaits for a broker confirm. The promise resolves once LavinMQ has the message. Pass{ confirm: false }to skip the wait.- Use
amqps://for TLS. The client verifies the server certificate and hostname, unless the URL carries aninsecurequery parameter.
The amqp-client.js README covers RPC, codecs, WebSockets and the low-level API.
Other Node.js clients
- amqplib, the most widely used Node.js AMQP client
- rabbitmq-client, a client with built-in reconnects and a promise-based API
Ready to take the next steps?
Managed LavinMQ instance via CloudAMQP
LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.
Get started with CloudAMQP ->Help and feedback
We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.