MqttConnection

MQTT connection using the default driver.

Inherits: IMessageQueueConnection, IReferenceable, IConfigurable, IOpenable

Description

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

Important points

By defining a connection and sharing it through multiple message queues you can reduce the number of used connections.

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 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 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 MqttConnection()

Fields

_clientId

The hostname as client id

protected _clientId: string

_connectTimeout

Connection timeout

protected _connectTimeout: int = 30000

_connection

MQTT connection pool object

protected _connection: IMqttClient

_connectionResolver

Connection resolver

protected _connectionResolver: MqttConnectionResolver

_keepAliveTimeout

Keep alive timeout

protected _keepAliveTimeout: int = 60000

_logger

Logger

protected _logger: CompositeLogger = new CompositeLogger()

_options

Connection options

protected _options: ConfigParams = new ConfigParams()

_reconnectTimeout

Reconnect timeout

protected _reconnectTimeout: int = 1000

_retryConnect

Retry option

protected _retryConnect: bool = true

_subscriptions

Topic subscriptions

protected _subscriptions: List<MqttSubscription> = new List<MqttSubscription>()

Instance methods

CheckOpen

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

private void CheckOpen()

CloseAsync

Closes a component and frees used resources.

public Task CloseAsync(string correlationId)

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

Configure

Configures the component by passing its configuration parameters.

public void Configure([ConfigParams](../../../commons/config/config_params config)

  • config: ConfigParams - configuration parameters to be set.

CreateQueueAsync

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

public Task CreateQueueAsync(string name)

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

DeleteQueueAsync

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

public Task DeleteQueueAsync(string name)

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

GetConnection

Gets the connection.

public IMqttClient GetConnection()

  • returns: IMqttClient - connection to an MQTT broker.

IsOpen

Checks if the component is open.

public bool IsOpen()

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

OpenAsync

Opens the component.

public Task OpenAsync(string correlationId)

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

PublishAsync

Publish a message to a specified topic.

public Task PublishAsync(string topic, MqttApplicationMessage message)

  • topic: string - topic name
  • message: MqttApplicationMessage - message to be published

ReadQueueNamesAsync

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

public Task<List<string>> ReadQueueNamesAsync()

  • returns: Task<List<string>> - queue names.

SetReferences

Sets references to dependent components.

public void SetReferences(IReferences references)

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

SubscribeAsync

Subscribes to a topic

public Task SubscribeAsync(string topic, MqttQualityOfServiceLevel qos, IMqttMessageListener listener)

  • topic: string - topic name
  • qos: MqttQualityOfServiceLevel - quality of service level
  • listener: IMqttMessageListener - message listener

UnsubscribeAsync

Unsubscribes from a previously subscribed topic.

public Task UnsubscribeAsync(string topic, IMqttMessageListener listener)

See also