MqttConnection

MQTT connection using the default driver.

Implements: IMessageQueueConnection, IReferenceable, IConfigurable, IOpenable

Description

The MqttConnection class allows you to create MQTT connections using the default driver.

Configuration parameters

  • client_id: (optional) name of the client id
  • 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:
    • retry_connect: (optional) turns on/off automated reconnect when connection is log (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 broker while inactive (default: 3000)

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) [ICounters]ICounters components to pass collected measurements
  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:credential-store:*:*:1.0 (optional) credential stores to resolve credentials

Constructors

Creates a new instance of the connection component.

public constructor()

Fields

_clientId

The hostname as client id

protected _clientId: string

_connectTimeout

Connection timeout

protected _connectTimeout: number = 30000

_connection

MQTT connection pool object

protected _connection: any

_connectionResolver

Connection resolver

protected _connectionResolver: MqttConnectionResolver

_keepAliveTimeout

Keep alive timeout

protected _keepAliveTimeout: number = 60000

_logger

Logger

protected _logger: CompositeLogger = new CompositeLogger()

_options

Connection options

protected _options: ConfigParams = new ConfigParams()

_reconnectTimeout

Reconnect timeout

protected _reconnectTimeout: number = 1000

_retryConnect

Retry option

protected _retryConnect: boolean = true

_subscriptions

Topic subscriptions

protected _subscriptions: MqttSubscription[] = []

Instance methods

checkOpen

Checks if the connection is open.
Raises an error if the connection is closed.

protected checkOpen(): void

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.

configure

Configures the component by passing its configuration parameters.

public configure(config: ConfigParams): void

  • config: ConfigParams - configuration parameters to be set.

createQueue

Creates a message queue. If the connection doesn’t support this function, it exists without error.

public createQueue(name: string): Promise<void>

  • name: string - name of the queue to be created.

deleteQueue

Deletes a message queue. If connection doesn’t support this function, it exists without error.

public deleteQueue(name: string): Promise<void>

  • name: string - name of the queue to be deleted.

getConnection

Gets the connection.

public getConnection(): any

  • returns: any - connection to a MySQL database

isOpen

Checks if the component is open.

public isOpen(): boolean

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

open

Opens the component.

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

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

publish

Publish a message to a specified topic.

public publish(topic: string, data: Buffer, options: any): Promise<void>

  • topic: string - topic name
  • data: Buffer - message to be published
  • options: any - publishing options

readQueueNames

Reads a list of registered queue names. If the connection doesn’t support this function, it returns an empty list.

public readQueueNames(): Promise<string[]>

  • returns: Promise<string[]> - queue names.

setReferences

Sets references to dependent components.

public setReferences(references: IReferences): void

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

subscribe

Subscribes to a topic

public subscribe(topic: string, options: any, listener: IMqttMessageListener): Promise<void>

  • topic: string - topic name
  • options: any - subscription options
  • listener: IMqttMessageListener - message listener

unsubscribe

Unsubscribes from a previously subscribed topic.

public unsubscribe(topic: string, listener: IMqttMessageListener: Promise<void>

See also