The AMQP client JavaScript has been waiting for

For years, connecting JavaScript to an AMQP broker meant callbacks, a pile of dependencies, and writing your own reconnection logic. amqp-client.js just shipped its biggest release yet: a small, fast, safe AMQP client with zero dependencies that runs in any JavaScript runtime, Node.js, Bun, Deno, and the browser.

amqp-client.js isn't new. It's been a lean, low-level AMQP client for years. What's new is version 4.0, which adds a high-level API that handles the hard parts for you, reconnection, consumer recovery, RPC, and message encoding. With 4.0, using AMQP from JavaScript is finally a pleasure, and the obvious choice for any new project. We've already switched our own services over to it and deleted hundreds of lines of boilerplate in the process.

When we launched amqp-client.rb 2.0, we wrote about our commitment to great AMQP clients and said JavaScript was next in line. Version 4.0 is us delivering on that.

JavaScript and AMQP

JavaScript developers have never been short of ways to talk to an AMQP broker, and amqplib has long been the default. It's a capable library, but it's callback-oriented, pulls in more than a dozen transitive dependencies, and leaves reconnection entirely up to you. Every production app ends up shipping its own reconnect loop and its own code to re-declare queues and re-attach consumers. And JavaScript runs everywhere: the browser, servers, edge functions, CLIs. An AMQP client should feel at home in all of them and set a high bar for speed, safety, and developer experience. That's exactly what amqp-client.js is built for.

CloudAMQP amqp-client.js

amqp-client.js is CloudAMQP's open-source AMQP 0-9-1 client, written in TypeScript and supported by the team that runs RabbitMQ and LavinMQ for thousands of customers. Here's what makes it worth a look:

It's small and fast

The whole client is roughly 1,700 lines of code with zero runtime dependencies. For comparison, amqplib ships around a dozen dependencies. Less code and a smaller dependency tree mean faster installs, smaller bundles, and less surface area to audit. You don't trade functionality for it.

It reconnects for you

The high-level AMQPSession API connects, and reconnects automatically with exponential backoff if the connection drops. Your consumers are re-attached on their own, so a blip in the network doesn't mean a restart. RPC and an opt-in codec registry (JSON and text, gzip and deflate) are built in.

It's safe by default

Messages are published as persistent by default, so they survive a broker restart when the queue is durable. Publishes respect back-pressure and aren't considered done until the data is on the wire, or until the broker confirms when publish confirms are enabled. Being TypeScript-first, a lot of mistakes get caught at compile time instead of in production.

It runs anywhere JavaScript does

The same client runs in Node.js, Bun, Deno, and the browser. It picks TCP or WebSockets automatically from the connection URL, so a browser talks to the broker over WebSockets with no code changes. All CloudAMQP servers ship the WebSocket relay, so there's nothing extra to install.

Get started with JavaScript and amqp-client.js

Usage

The client has two APIs. A low-level API that maps closely to the AMQP protocol, does everything the protocol allows, and leaves connection management to you. And a high-level API that's easier to start with and handles reconnection automatically.

High-level API

import { AMQPSession, builtinParsers, builtinCoders } from "@cloudamqp/amqp-client"

// Connects, and reconnects automatically if the connection is lost.
// The codec registry encodes and decodes message bodies for you.
const session = await AMQPSession.connect("amqp://localhost", {
  parsers: builtinParsers,
  coders: builtinCoders,
  defaultContentType: "application/json",
})

// Declare a durable queue
const queue = await session.queue("my_queue")

// Publish an object directly. It's serialized to JSON automatically (persistent by default).
await queue.publish({ task: "resize", id: 42 })

// Subscribe. msg.body is already decoded, no JSON.parse needed.
// Acked when the callback returns, nacked + requeued if it throws. Survives reconnects.
await queue.subscribe({ prefetch: 20 }, async (msg) => {
  process(msg.body)
})

// Bind the queue to an exchange with a routing key
await queue.bind("amq.topic", "my.events.*")

Low-level API

import { AMQPClient } from "@cloudamqp/amqp-client"

// Open and establish a connection
const amqp = new AMQPClient("amqp://localhost")
const conn = await amqp.connect()

// Open a channel and declare a queue
const ch = await conn.channel()
const q = await ch.queueDeclare("my_queue")

// Publish a persistent message
await ch.basicPublish("", q.name, "Hello World", { deliveryMode: 2 })

// Poll the queue for a message
const msg = await ch.basicGet(q.name)
console.log(msg.bodyToString())

await conn.close()

Installation

Add amqp-client.js to your project:

$ npm install @cloudamqp/amqp-client --save

Upgrading from earlier versions

The high-level API arrived in version 4.0, which also tightened a few things up. The breaking changes worth knowing about:

  • AMQPChannel.queue() is removed. Use session.queue() for the high-level API, or channel.queueDeclare() with the low-level channel methods.
  • AMQPQueue is now a session-level handle rather than a channel-level one, so it survives reconnects.
  • Publishes default to persistent now. Pass deliveryMode: 1 if you want transient messages.
  • The minimum supported Node.js version is 18.

The CHANGELOG has a full before/after migration example.

For a full getting-started guide, see the JavaScript documentation Further reading and links:

  1. GitHub repository: https://github.com/cloudamqp/amqp-client.js
  2. API reference documentation: https://cloudamqp.github.io/amqp-client.js/

If you have any questions or comments about the client, send us an email at contact@cloudamqp.com

Until next time,
CloudAMQP team

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies