MqttClient class
A client class for interacting with MQTT Data Packets.
Do not instantiate this class directly, instead instantiate
either a MqttClientServer
class or an MqttBrowserClient as needed.
This class now provides common functionality between server side
and web based clients.
- Implementers
Constructors
- MqttClient.new(String server, String clientIdentifier)
- Initializes a new instance of the MqttClient class using the default Mqtt Port. The server hostname to connect to The client identifier to use to connect with
- MqttClient.withPort(String server, String clientIdentifier, int port)
- Initializes a new instance of the MqttClient class using the supplied Mqtt Port. The server/hostname to connect to. The client identifier to use to connect with. The port to use
Properties
- autoReconnect ↔ bool
-
Auto reconnect, the client will auto reconnect if set true.
getter/setter pair
- averageCycleLatency → int?
-
The average latency of all ping/pong cycles in a connection period in
milliseconds. Cleared on disconnect.
no setter
- clientEventBus ↔ EventBus?
-
The event bus
getter/setter pair
- clientIdentifier ↔ String
-
Client identifier
getter/setter pair
- connectionHandler ↔ MqttConnectionHandlerBase?
-
The Handler that is managing the connection to the remote server.
getter/setter pair
- connectionMessage ↔ MqttConnectMessage?
-
getter/setter pair
- connectionState → MqttConnectionState?
-
Gets the current connection state of the Mqtt Client.
Will be removed, use connectionStatus
no setter
- connectionStatus → MqttClientConnectionStatus?
-
Gets the current connection status of the Mqtt Client.
This is the connection state as above also with the broker return code.
Set after every connection attempt.
no setter
- connectTimeoutPeriod ↔ int
-
getter/setter pair
- disconnectOnNoResponsePeriod ↔ int
-
The period of time to wait if the broker does not respond to a ping request
from keep alive processing, in seconds.
If this time period is exceeded the client is forcibly disconnected.
The default is 0, which disables this functionality.
Thi setting has no effect if keep alive is disabled.
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- instantiationCorrect ↔ bool
-
Incorrect instantiation protection
getter/setter pair
- keepAlive ↔ MqttConnectionKeepAlive?
-
Handles the connection management while idle.
Not instantiated if keep alive is disabled.
getter/setter pair
- keepAlivePeriod ↔ int
-
Keep alive period, seconds.
Keep alive is defaulted to off, this must be set to a valid value to
enable keep alive.
getter/setter pair
- lastCycleLatency → int?
-
The latency of the last ping/pong cycle in milliseconds.
Cleared on disconnect.
no setter
- manuallyAcknowledgeQos1 ↔ bool
-
getter/setter pair
- messagesAwaitingManualAcknowledge → int
-
The number of QOS 1 messages awaiting manual acknowledge.
no setter
- onAutoReconnect ↔ AutoReconnectCallback?
-
Auto reconnect callback, if auto reconnect is selected this callback will
be called before auto reconnect processing is invoked to allow the user to
perform any pre auto reconnect actions.
getter/setter pair
- onAutoReconnected ↔ AutoReconnectCompleteCallback?
-
Auto reconnected callback, if auto reconnect is selected this callback will
be called after auto reconnect processing is completed to allow the user to
perform any post auto reconnect actions.
getter/setter pair
- onConnected ↔ ConnectCallback?
-
Client connect callback, called on successful connect
getter/setter pair
- onDisconnected ↔ DisconnectCallback?
-
Client disconnect callback, called on unsolicited disconnect.
This will not be called even if set if [autoReconnect} is set,instead
AutoReconnectCallback will be called.
getter/setter pair
- onFailedConnectionAttempt ↔ FailedConnectionAttemptCallback?
-
Failed Connection attempt callback.
Called on every failed connection attempt, if
maxConnectionAttempts
is set to 5 say this will be called 5 times if the connection fails, one for every failed attempt. Note this is never called if autoReconnect is set, also the NoConnectionException is not raised if this callback is supplied.getter/setter pair - onSubscribed ↔ SubscribeCallback?
-
On subscribed
getter/setter pair
- onSubscribeFail ↔ SubscribeFailCallback?
-
On subscribed fail
getter/setter pair
- onUnsubscribed ↔ UnsubscribeCallback?
-
On unsubscribed
getter/setter pair
- pingCallback ↔ PingCallback?
-
The ping sent callback
getter/setter pair
- pongCallback ↔ PongCallback?
-
The ping received callback
getter/setter pair
- port ↔ int
-
Port number
getter/setter pair
-
published
→ Stream<
MqttPublishMessage> ? -
Published message stream. A publish message is added to this
stream on completion of the message publishing protocol for a Qos level.
Attach listeners only after connect has been called.
no setter
- publishingManager ↔ PublishingManager?
-
Handles everything to do with publication management.
getter/setter pair
- resubscribeOnAutoReconnect ↔ bool
-
Re subscribe on auto reconnect.
Auto reconnect will perform automatic re subscription of existing confirmed subscriptions
unless this is set to false.
In this case the caller must perform their own re subscriptions manually using unsubscribe,
subscribe and resubscribe as needed from the appropriate callbacks.
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- server ↔ String
-
Server name.
Note that a server name that is a host name must conform to the name
syntax described in RFC952
https://datatracker.ietf.org/doc/html/rfc952
getter/setter pair - socketTimeout ↔ int?
-
getter/setter pair
- subscriptionsManager ↔ SubscriptionsManager?
-
The subscriptions manager responsible for tracking subscriptions.
getter/setter pair
-
updates
→ Stream<
List< ?MqttReceivedMessage< >MqttMessage> > -
The stream on which all subscribed topic updates are published to
no setter
-
websocketProtocols
← List<
String> -
User definable websocket protocols. Use this for non default websocket
protocols only if your broker needs this. There are two defaults in
MqttWsConnection class, the multiple protocol is the default. Some brokers
will not accept a list and only expect a single protocol identifier,
in this case use the single protocol default. You can supply your own
list, or to disable this entirely set the protocols to an
empty list , i.e [].
no getter
-
websocketProtocolString
↔ List<
String> ? -
getter/setter pair
Methods
-
acknowledgeQos1Message(
MqttPublishMessage message) → bool? - Manually acknowledge a received QOS 1 message. Has no effect if manuallyAcknowledgeQos1 is not in force or the message is not awaiting a QOS 1 acknowledge. Returns true if an acknowledgement is sent to the broker.
-
checkCredentials(
String? username, String? password) → void - Check the username and password validity
-
connect(
[String? username, String? password]) → Future< MqttClientConnectionStatus?> - Common client connection method.
-
disconnect(
) → void - Disconnect from the broker. This is a hard disconnect, a disconnect message is sent to the broker and the client is then reset to its pre-connection state, i.e all subscriptions are deleted, on subsequent reconnection the use must re-subscribe, also the updates change notifier is re-initialised and as such the user must re-listen on this stream.
-
disconnectOnNoMessageSent(
DisconnectOnNoMessageSent event) → void - Called when the send message function throws exception a ping response expected from the broker has not arrived in the time period specified by disconnectOnNoResponsePeriod.
-
disconnectOnNoPingResponse(
DisconnectOnNoPingResponse event) → void - Called when the keep alive mechanism has determined that a ping response expected from the broker has not arrived in the time period specified by disconnectOnNoResponsePeriod.
-
doAutoReconnect(
{bool force = false}) → void -
Auto reconnect method, used to invoke a manual auto reconnect sequence.
If autoReconnect is not set this method does nothing.
If the client is not disconnected this method will have no effect
unless the
force
parameter is set to true, otherwise auto reconnect will try indefinitely to reconnect to the broker. -
getConnectMessage(
String? username, String? password) → MqttConnectMessage - Gets a pre-configured connect message if one has not been supplied by the user. Returns an MqttConnectMessage that can be used to connect to a message broker if the user has not set one.
-
getSubscriptionsStatus(
String topic) → MqttSubscriptionStatus - Gets the current status of a subscription.
-
internalDisconnect(
) → void - Internal disconnect This is always passed to the connection handler to allow the client to close itself down correctly on disconnect.
-
logging(
{required bool on}) → void - Turn on logging, true to start, false to stop
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
publishMessage(
String topic, MqttQos qualityOfService, Uint8Buffer data, {bool retain = false}) → int - Publishes a message to the message broker. Returns The message identifer assigned to the message. Raises InvalidTopicException if the topic supplied violates the MQTT topic format rules.
-
resubscribe(
) → void - Re subscribe. Unsubscribes all confirmed subscriptions and re subscribes them without sending unsubscribe messages to the broker. If an unsubscribe message to the broker is needed then use unsubscribe followed by subscribe for each subscription. Can be used in auto reconnect processing to force manual re subscription of all existing confirmed subscriptions.
-
setProtocolV31(
) → void - Set the protocol version to V3.1 - default
-
setProtocolV311(
) → void - Set the protocol version to V3.1.1
-
subscribe(
String topic, MqttQos qosLevel) → Subscription? - Initiates a topic subscription request to the connected broker with a strongly typed data processor callback. The topic to subscribe to. The qos level the message was published at. Returns the subscription or null on failure
-
toString(
) → String -
A string representation of this object.
inherited
-
unsubscribe(
String topic, {dynamic expectAcknowledge = false}) → void -
Unsubscribe from a topic.
Some brokers(AWS for instance) need to have each un subscription acknowledged, use
the
expectAcknowledge
parameter for this, default is false.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited