Connection class - abreits/amqp-ts GitHub Wiki

Connection class

The connection class defines the connection with the AMQP server.

Contains

methods
properties

Detailed reference

A detailed reference explaining the meaning and use of each method and property.

constructor

constructor ( url?: string,
              socketOptions?: any,
              reconnectStrategy?: ReconnectStrategy)

Creates a new connection to an AMQP server

parameters
  • url?: string : amqp connection string in amqplib.connect format, defaults to "amqp://localhost".
  • socketOptions?: any : socket options as explained in amqplib.connect.
  • reconnectStrategy?: ReconnectStrategy : defines the reconnection strategy used, defaults to {retries: 0 //forever//, interval: 1500 //ms//}.
example
import * as Amqp from "amqp-ts";

var connection = new Amqp.Connection("amqp://localhost?heartbeat=60");

connection.close

connection.close (): Promise<void>

Closes the connection

result
  • Promise<void> : promise that resolves when the connection is closed.
example
connection.close();

connection.declareExchange

connection.declareExchange ( name: string,
                             type?: string,
                             options?: Exchange.DeclarationOptions)
                           : Exchange

Connect to the exchange on the server and create it if it does not already exist. If an exchange with the same name is already declared for this connection, the already declared exchange instance is returned and type and options parameters are ignored.

parameters
  • name: string : exchange name.
  • type?: string : exchange type, a valid AMQP exchange type name.
  • options?: Exchange.DeclarationOptions : exchange options as defined in amqplib. An extra exchange declaration option has been added in v1.3: noCreate, this connects to an already existing AMQP exchange name, ignoring the exchange type and all other declaration options.
result
  • Exchange : the declared exchange.
example
connection.declareExchange("exchangeName", "amq.topic", {durable: false});
// expect an existing exchange
connection.declareExchange("existingExchangeName", "", {noCreate: true});

connection.declareQueue

connection.declareQueue ( name: string,
                          options?: Queue.DeclarationOptions)
                        : Queue

Connect to the queue on the server and create it if it does not already exist. If a queue with the same name is already declared for this connection, the already declared queue instance is returned and type and options parameters are ignored.

parameters
  • name: string : queue name.
  • options?: Queue.DeclarationOptions : exchange options as defined in amqplib. An extra queue declaration option has been added in v1.3: noCreate, this connects to an already existing AMQP queue name, ignoring all other declaration options.
result
  • Queue : the declared queue.
example
connection.declareQueue("queueName", {durable: false});
// expect an existing queue
connection.declareQueue("existingQueueName", {noCreate: true});

connection.declareTopology

connection.declareTopology (topology: Connection.Topology): Promise<void>

Declare a topology, consisting of zero or more Exchanges, Queues and Bindings.

result
  • Promise<any> : promise that resolves when all declared exchanges, queues and bindings for the topology are resolved.
example
var topology = {
    exchanges: [
        {name: "exchange1", type: "direct", options: {durable: false}},
        {name: "exchange2"}
    ],
    queues: [
        {name: "queue1", options: {messageTtl: 60000}},
        {name: "queue2"}
    ],
    bindings: [
        {source: "exchange1", queue: "queue1", pattern: "debug", args: {}},
        {source: "exchange1", exchange: "exchange2", pattern: "error"},
        {source: "exchange2", queue: "queue2"}
    ]
};

connection.declareTopology(topology).then() => {
    // do things when everything is in place
}

connection.completeConfiguration

connection.completeConfiguration (): Promise<void>

Makes sure every defined Exchange, Queue and Binding for this Connection is resolved.

result
  • Promise<any> : promise that resolves when all defined exchanges, queues and bindings for the connection are resolved.
example
connection.completeConfiguration().then(() => {
    // do things when everything is in place
});

connection.deleteConfiguration

connection.deleteConfiguration (): Promise<void>

Deletes every defined Exchange, Queue and Binding defined in this Connection.
warning: this deletes the exchanges, queues and bindings from the AMQP server, even if they already existed before.

result
  • Promise<any> : promise that resolves when all defined exchanges, queue and bindings in the connection have been deleted.
example
    connection.deleteConfiguration().then(() => {
        // everything we created has been removed from the server
        connection.close();
    });

connection.initialized

connection.initialized: Promise<void>

indicates whether the connection initialization is resolved (or rejected)

example
connection.initialized.then(() => {
    // stuff to do
}
connection.initialized.catch((err) => {
    // something went wrong
}
⚠️ **GitHub.com Fallback** ⚠️