Home - abreits/amqp-ts GitHub Wiki

amqp-ts (AMQP TypeScript)

Table of Contents

Overview

Amqp-ts is a library for nodejs that simplifies communication with AMQP message busses written in Typescript. It has been tested on RabbitMQ. It uses the amqplib library by Michael Bridgen (squaremo).

Important Changes

Starting in version 0.14 the return type of [exchange.rpc](https://github.com/abreits/amqp-ts/wiki/Exchange class#rpc) and [queue.rpc](https://github.com/abreits/amqp-ts/wiki/Queue class#rpc) changed from 'Promise < any >' to 'Promise < [Message](https://github.com/abreits/amqp-ts/wiki/Message class) >'.

Starting in version 0.12 the [Message class](https://github.com/abreits/amqp-ts/wiki/Message class) has been added. It is a more elegant way to send and receive messages. It is the preferred way to deal with sending and receiving messages.

Defining Features

  • High level non opinionated library, no need to worry about channels etc, if you need to access channels directly use amqplib.
  • 'Lazy' initialization, async AMQP dependencies are resolved automatically
  • Automatic reconnection, when the connection with the AMQP server fails, the whole connection and configuration is rebuilt automatically
  • Written in typescript, it is compatible with the Typescript 1.6 module type definition resolution for node.js.

Current status

The library is considered production ready.

It does depend on the following npm libraries:

The DefinitelyTyped tsd tool is used to manage the typescript type definitions.

Lazy Initialization

No need to nest functionality, just create a connection, declare your exchanges, queues and bindings and send and receive messages. The library takes care of any direct dependencies.

If you define an exchange and a queue and bind the queue to the exchange and want to make sure that the queue is connected to the exchange before you send a message to the exchange you can call the connection.completeConfiguration() method and act on the promise it returns.

ES6/Typescript Example
import * as Amqp from "amqp-ts";

var connection = new Amqp.Connection("amqp://localhost");
var exchange = connection.declareExchange("ExchangeName");
var queue = connection.declareQueue("QueueName");
queue.bind(exchange);
queue.activateConsumer((message) => {
    console.log("Message received: " + message.getContent());
});

// it is possible that the following message is not received because
// it can be sent before the queue, binding or consumer exist
var msg = new Amqp.Message("Test");
exchange.send(msg);

connection.completeConfiguration().then(() => {
    // the following message will be received because
    // everything you defined earlier for this connection now exists
    var msg2 = new Amqp.Message("Test2");
    exchange.send(msg2);
});
Javascript Example
var amqp = require("amqp-ts");

var connection = new amqp.Connection("amqp://localhost");
var exchange = connection.declareExchange("ExchangeName");
var queue = connection.declareQueue("QueueName");
queue.bind(exchange);
queue.activateConsumer((message) => {
    console.log("Message received: " + message.getContent());
});

// it is possible that the following message is not received because
// it can be sent before the queue, binding or consumer exist
var msg = new amqp.Message("Test");
exchange.send(msg);

connection.completeConfiguration().then(() => {
    // the following message will be received because
    // everything you defined earlier for this connection now exists
    var msg2 = new amqp.Message("Test2");
    exchange.send(msg2);
});

More examples can be found in the tutorials directory.

Automatic Reconnection

When the library detects that the connection with the AMQP server is lost, it tries to automatically reconnect to the server.

⚠️ **GitHub.com Fallback** ⚠️