Net::MQTT
NAME
Net::MQTT
SYNOPSIS
my $client = Net::MQTT.new(:server<localhost>);
react {
whenever $client.subscribe('raku/updates/#') -> $update {
say "{$update.topic}: {$update.payload.decode('utf8-c8')}";
}
whenever Supply.interval(20) {
$client.publish('time/now', "Current time is {Datetime.now}", :retain);
}
whenever $client.connected {
say "Connected";
LAST { done }
}
}
DESCRIPTION
Net::MQTT is an implementation of a MQTT client.
METHODS
new(*%args)
This creates a new mqtt client. It takes the follow arguments, all optional unless indicated otherwise:
Str :$server
The name of the server to connect to. This argument is mandatory
Str :$client-identifier
An unique identifier for this client/session. If none is given a random identifier will be generated.
Bool :$tls
If true this enables tls. This requires `IO::Socket::Async::SSL`.
Int :$port
This sets the port, it defaults to 1883 if tls is disabled, and 8883 if it is enabled
Int :$keep-alive-interval
This sets the keep-alive interval (in seconds). This defaults to
60.Int :$resend-interval
This sets the resend interval (in seconds). This defaults to
5.Int :$connect-interval
This sets the connect interval. It defaults to the resend interval.
Int :$reconnect-attempts
This sets how many times it will try to (re)connect before giving up. This defaults to
3.Str :$username
This sets the username, if any.
Str :$password
This sets the password, if any.
Protocol::MQTT::Message :$will
This is the will of the connection. If the connection is lost without a formal disconnect, the server will send this message as if the client sent it.
publish(Str $topic, Str $payload, Bool :$retain, Qos :$qos --> Promise)
This publishes $payload to $topic, with quality-of-service $qos (defaulting to At-most-once). If $retain is true, it will be a retained message. The resulting Promise will be true once the confirmation of reception has been received (or immediately for At-most-once).
subscribe(Str $topic, Qos :$qos --> Supply)
Subscribe to $topic, asking for messages to be sent with quality-of-service $qos (defaulting to At-most-once).
unsubscribe($topic -> Promise)
Unsubscribe from $topic.
disconnect(--> Nil)
This will send a disconnect message to the server, and then close the connection.
connected(--> Supply)
This supply will emit a value whenever a connection has been established. It will end when disconnect is called, or if a connection breaks and can't be re-established.
disconnected(--> Supply)
This supply will emit a value whenever a connection has been lost.
DEPENDENCIES
This module has an optional dependency on IO::Socket::Async::SSL for TLS support.