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.

MqttConnection()

Fields

clientId_

The hostname as client id

clientId_: String

connectTimeout_

Connection timeout

connectTimeout_: int = 30000

connection_

MQTT connection pool object

connection_: mqtt.MqttServerClient?

connectionResolver_

Connection resolver

connectionResolver_: MqttConnectionResolver

keepAliveTimeout_

Keep alive timeout

keepAliveTimeout_: int = 60000

logger_

Logger

logger_: CompositeLogger = CompositeLogger()

options_

Connection options

options_: ConfigParams = ConfigParams()

reconnectTimeout_

Reconnect timeout

reconnectTimeout_: int = 1000

retryConnect_

Retry option

retryConnect_: bool = true

subscriptions_

Topic subscriptions

subscriptions_: List<MqttSubscription>

Instance methods

checkOpen

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

void checkOpen()

close

Closes a component and frees used resources.

@override

Future close(String? correlationId)

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

configure

Configures the component by passing its configuration parameters. @override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

createQueue

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

@override

Future<void> createQueue(String name)

  • 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.

@override

Future<void> deleteQueue(String name)

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

getConnection

Gets the connection.

mqtt.MqttServerClient? getConnection()

  • returns: mqtt.MqttServerClient? - connection to a MySQL database

isOpen

Checks if the component is open. @override

bool isOpen()

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

open

Opens the component. @override

Future open(String? correlationId)

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

publish

Publish a message to a specified topic.

Future publish(String topic, String data, Map<String, dynamic> options)

  • topic: String - topic name
  • data: String - message to be published
  • options: Map<String, dynamic> - publishing options

readQueueNames

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

@override

Future<List<String>> readQueueNames()

  • returns: Future<List<String>> - queue names.

setReferences

Sets references to dependent components. @override

void setReferences(IReferences references)

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

subscribe

Subscribes to a topic

Future subscribe(String topic, Map<String, dynamic> options, IMqttMessageListener listener)

  • topic: String - topic name
  • options: Map<String, dynamic> - subscription options
  • listener: IMqttMessageListener - message listener

unsubscribe

Unsubscribes from a previously subscribed topic.

Future unsubscribe(String topic, IMqttMessageListener listener)

See also