Skip to content
abreits edited this page Nov 5, 2015 · 18 revisions

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).

Defining Features
  • [High level non opinioned library], no need to worry about channels etc.
  • '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 resulution for node.js.
Current status

This is a work in progress, currently in a late beta state.

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.bind("QueueName");
queue.bind(exchange);
queue.startConsumer((message) => {
    console.log("Message received: " + message);
}

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

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

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

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

connection.completeConfiguration().then(function () {
    // the following message will be received because
    // everything you defined earlier for this connection now exists
    exchange.send("Test2");
});

Automatic Reconnection

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

This is still an experimental feature and has not been thoroughly tested.

Logging

TODO: describe winston configuration. Maybe changed or removed in future versions.

Clone this wiki locally