Message class - abreits/amqp-ts GitHub Wiki

Message class

The Message class defines an AMQP message.

Contains

methods
properties

Detailed reference

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

constructor

constructor ( content?: any,
              options?: any)

Creates a new message.

parameters
  • content?: any : message content.
  • options?: any : message options, as defined in amqplib, defaults to {}.
example
import * as Ampq from "amqp-ts";
msg1 = new Amqp.Message("TestMessage");
msg2 = new Amqp.Message({name: "TemporaryTest", value: 12345}, {expiration: 100000});

message.setContent

setContent ( content: any );

Replaces the message content with the given content. It transforms the given content to a Buffer:

  • Buffer : send the content as is (no preprocessing).
  • string : create a Buffer from the string and send that buffer.
  • everything else : create a Buffer from the to JSON converted object and, if not defined, set the contentType option to "application/json".
parameters
  • content?: any : new message content.
example
message.setContent("TestMessage");

message.getContent

message.getContent (): any

Returns the content from the content property and converts it to a javascript structure if the contentType property is set to application/json, otherwise it converts the content to a string.

result
  • any : converted content buffer.
example
var msgContent = message.getContent();

message.sendTo

message.sendTo ( destination: Exchange | Queue,
                  routingKey?: string)

Send the message to an exchange or queue.

example
message.sendTo(exchange);

message.ack

message.ack (allUpTo?: boolean)

Acknowledges a message. Only works inside consumer function of [queue.activateConsumer](Queue class#activateConsumer) or [exchange.ActivateConsumer](Exchange class#activateConsumer).

parameters
  • allUpTo?: boolean : acknowledge all messages upto this one, defaults to false.
example
queue.activateConsumer((msg) => {
    //process the msg
    msg.ack();
});

message.nack

message.nack (allUpTo?: boolean, requeue?: boolean)

Does explicitly not acknowledge the message. Only works inside consumer function of [queue.activateConsumer](Queue class#activateConsumer) or [exchange.ActivateConsumer](Exchange class#activateConsumer).

parameters
  • allUpTo?: boolean : nacks all messages upto this one, defaults to false.
  • requeue?: boolean : requeue the nacked message, defaults to true.
example
queue.activateConsumer((msg) => {
    //process the msg
    if(we_cannot_process_this_message) {
        msg.nack();
    } else {
        msg.ack();
    }
});

message.reject

message.reject (requeue?: boolean)

Does reject the message. Only works inside consumer function of [queue.activateConsumer](Queue class#activateConsumer) or [exchange.ActivateConsumer](Exchange class#activateConsumer).

parameters
  • requeue?: boolean : requeue the rejected message, defaults to true.
example
queue.activateConsumer((msg) => {
    //process the msg
    if(we_cannot_process_this_message) {
        msg.reject();
    } else {
        msg.ack();
    }
});

message.content

message.content: Buffer

Buffer that contains the content of the message that is sent or received.

message.fields

message.fields: any

Structure that contains the fields of the message that is received.

message.properties

message.properties: any

Structure that contains the properties of the message that is received or the options of the message that will be sent.