MqttMessageQueue

Message queue that sends and receives messages via an MQTT message broker.

Extends: MessageQueue

Implements: IReferenceable, IUnreferenceable, IConfigurable, IOpenable, ICleanable

Description

The MqttMessageQueue class allows you to create message queues that send and receive messages via an MQTT message broker.

Configuration parameters

  • topic: name of MQTT topic to subscribe
  • connection(s):
    • discovery_key: (optional) key to retrieve the connection from IDiscovery
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it
  • credential(s):
    • store_key: (optional) key to retrieve the credentials from ICredentialStore
    • username: username
    • password: user’s password
  • options:
    • serialize_envelope: (optional) true to serialize entire message as JSON, false to send only message payload (default: true)
    • autosubscribe: (optional) true to automatically subscribe on option (default: false)
    • qos: (optional) quality of service level aka QOS (default: 0)
    • retain: (optional) retention flag for published messages (default: false)
    • retry_connect: (optional) turns on/off automated reconnect when connection is lost (default: true)
    • connect_timeout: (optional) number of milliseconds to wait for connection (default: 30000)
    • reconnect_timeout: (optional) number of milliseconds to wait on each reconnection attempt (default: 1000)
    • keepalive_timeout: (optional) number of milliseconds to ping the broker while inactive (default: 3000)

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:credential-store:*:*:1.0 - (optional) ICredentialStore to resolve credentials
  • *:connection:mqtt:*:1.0 - (optional) shared connection to MQTT service

Constructors

Creates a new instance of the message queue.

public constructor(name?: string)

  • name: string - (optional) queue’s name.

Fields

_autoSubscribe

Auto-subscribe option

protected _autoSubscribe: boolean

_connection

MQTT connection component

protected _connection: any

_dependencyResolver

Dependency resolver

protected _dependencyResolver: DependencyResolver

_logger

Logger

protected _logger: CompositeLogger = new CompositeLogger()

_options

Configuration options

protected _options: ConfigParams = new ConfigParams()

_messages

Message

protected _messages: MessageEnvelope[] = []

_qos

Quality of service

protected _qos: number

_receiver

Message receiver

protected _receiver: IMessageReceiver

_serializeEnvelope

Serialization option

protected _serializeEnvelope: boolean

_subscribed

Subscribe option

protected _subscribed: boolean

_topic

Topic

protected _topic: string

Instance methods

abandon

Returnes a message into the queue and makes it available for all subscribers to receive it again. This method is usually used to return a message which could not be processed at the moment to repeat the attempt. Messages that cause unrecoverable errors shall be removed permanently or/and send to dead letter queue.

  • Important: This method is not supported by MQTT.

public abandon(message: MessageEnvelope): Promise<void>

clear

Clears a component’s state.

public clear(context: string): Promise<void>

  • context: string - (optional) transaction id used to trace execution through the call chain.

close

Closes a component and frees used resources.

public close(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

complete

Permanently removes a message from the queue. This method is usually used to remove the message after successful processing.

  • Important: This method is not supported by MQTT.

public complete(message: MessageEnvelope): Promise<void>

configure

Configures a component by passing its configuration parameters.

public configure(config: ConfigParams): void

  • config:: ConfigParams - configuration parameters to be set.

endListen

Ends listening for incoming messages. When this method is call, listen unblocks the thread and execution continues.

public endListen(context: IContext): void

  • context: IContext - (optional) a context to trace execution through a call chain.

fromMessage

Returns the topic and the data of a message.

protected fromMessage(message: MessageEnvelope): any

getTopic

Obtains the topic.

protected getTopic(): string

  • returns: string - topic

isOpen

Checks if the component is open.

public isOpen(): boolean

  • returns: boolean - true if the component is open and false otherwise.

listen

Listens for incoming messages and blocks the current thread until the queue is closed.

See IMessageReceiver

public listen(context: IContext, receiver: IMessageReceiver): void

  • context: IContext - (optional) a context to trace execution through a call chain.
  • receiver: IMessageReceiver - receiver used to receive incoming messages.

moveToDeadLetter

Permanently removes a message from the queue and sends it to dead letter queue.

  • Important: This method is not supported by MQTT.

public moveToDeadLetter(message: MessageEnvelope): Promise<void>

onMessage

Checks if the message comes from the right topic. If this is the case, deserializes and sends it to the receiver if it’s set. Otherwise, puts it into the queue.

public onMessage(topic: string, data: any, packet: any): void

  • topic: string - topic
  • data: any - data
  • packet: any - packet

open

Opens the component.

public open(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

peek

Peeks a single incoming message from the queue without removing it. If there are no messages available in the queue, it returns null.

public peek(context: IContext): Promise<MessageEnvelope>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • returns: Promise<MessageEnvelope> - peeked message.

peekBatch

Peeks multiple incoming messages from the queue without removing them. If there are no messages available in the queue, it returns an empty list.

  • Important: This method is not supported by MQTT.

public peekBatch(context: IContext, messageCount: number): Promise<MessageEnvelope[]>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • messageCount: number - maximum number of messages to peek.
  • returns: Promise<MessageEnvelope[]> - list with peeked messages.

readMessageCount

Reads the current number of messages in the queue to be delivered.

public readMessageCount(): Promise<number>

  • *returns: Promise<number> - number of messages in the queue.

receive

Receives an incoming message and removes it from the queue.

public receive(context: IContext, waitTimeout: number): Promise<MessageEnvelope>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • waitTimeout: number - timeout in milliseconds to wait for a message to come.
  • returns: Promise<MessageEnvelope> - received message or null if nothing was received.

renewLock

Renews a lock on a message that makes it invisible from other receivers in the queue. This method is usually used to extend the message processing time.

  • Important: This method is not supported by MQTT.

public renewLock(message: MessageEnvelope, lockTimeout: number): Promise<void>

  • message: MessageEnvelope - message to extend its lock.
  • lockTimeout: number - locking timeout in milliseconds.

send

Sends a message into the queue.

public send(context: IContext, message: MessageEnvelope): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.
  • message: MessageEnvelope - message envelop to be sent.

setReferences

Sets references to dependent components.

public setReferences(references: IReferences): void

  • references: IReferences - references to locate the component’s dependencies.

subscribe

Subscribes to a topic.

protected subscribe(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

toMessage

If the message is null, it returns null. Otherwise, it returns the message.

protected toMessage(topic: string, data: any, packet: any): MessageEnvelope

  • topic: string - topic
  • data: any - data
  • packet: any - packet
  • returns: MessageEnvelope - Null if the message has no data. Otherwise, it returns the message.

unsetReferences

Unsets (clears) previously set references to dependent components.

public unsetReferences(): void

Examples

let queue = new MqttMessageQueue();
queue.configure(ConfigParams.fromTuples(
  "topic", "mytopic",
  "connection.protocol", "mqtt"
  "connection.host", "localhost"
  "connection.port", 1883
));

await queue.open("123");

await queue.send("123", new MessageEnvelope(null, "mymessage", "ABC"));
let message = await queue.receive("123");

if (message != null) {
    ...
    queue.complete("123", message);
}

See also