mqtt-hs-0.2.0: A MQTT client library.

CopyrightLukas Braun 2014
LicenseGPL-3
Maintainerkoomi+mqtt@hackerspace-bamberg.de
Safe HaskellNone
LanguageHaskell2010

Network.MQTT

Contents

Description

A MQTT client library.

A simple example, assuming a broker is running on localhost (needs -XOverloadedStrings):

>>> import Network.MQTT
>>> import Network.MQTT.Logger
>>> Just mqtt <- connect defaultConfig { cLogger = warnings stdLogger }
>>> let f t payload = putStrLn $ "A message was published to " ++ show t ++ ": " ++ show payload
>>> subscribe mqtt NoConfirm "#" f
NoConfirm
>>> publish mqtt Handshake False "some random/topic" "Some content!"
A message was published to "some random/topic": "Some content!"

Synopsis

Creating connections

connect :: MQTTConfig -> IO (Maybe MQTT) Source

Establish a connection. This might fail with an IOException or return Nothing if the server did not accept the connection.

data MQTT Source

Abstract type representing a connection to a broker.

disconnect :: MQTT -> IO () Source

Close the connection to the server.

reconnect :: MQTT -> Int -> IO () Source

Try creating a new connection with the same config (retrying after the specified amount of seconds has passed) and invoke the callback that is set with onReconnect once a new connection has been established.

Does not terminate the old connection.

onReconnect :: MQTT -> IO () -> IO () Source

Register a callback that will be invoked when a reconnect has happened.

resubscribe :: MQTT -> IO [QoS] Source

Resubscribe to all topics. Returns the new list of granted QoS.

Connection settings

data MQTTConfig Source

The various options when establishing a connection.

defaultConfig :: MQTTConfig Source

Defaults for MQTTConfig, connects to a server running on localhost.

Field accessors

cHost :: MQTTConfig -> HostName Source

Hostname of the broker.

cPort :: MQTTConfig -> PortNumber Source

Port of the broker.

cClean :: MQTTConfig -> Bool Source

Should the server forget subscriptions and other state on disconnects?

cWill :: MQTTConfig -> Maybe Will Source

Optional Will message.

cUsername :: MQTTConfig -> Maybe Text Source

Optional username used for authentication.

cPassword :: MQTTConfig -> Maybe Text Source

Optional password used for authentication.

cKeepAlive :: MQTTConfig -> Maybe Int Source

Maximum interval (in seconds) in which a message must be sent. 0 means no limit.

cClientID :: MQTTConfig -> Text Source

Client ID used by the server to identify clients.

cConnectTimeout :: MQTTConfig -> Maybe Int Source

Time in seconds after which waiting for a CONNACK is aborted. Nothing means no timeout.

cReconnPeriod :: MQTTConfig -> Maybe Int Source

Time in seconds to wait between reconnect attempts. Nothing means no reconnects are attempted.

cLogger :: MQTTConfig -> Logger Source

Functions for logging, see Logger.

Subscribing and publishing

subscribe :: MQTT -> QoS -> Topic -> (Topic -> ByteString -> IO ()) -> IO QoS Source

Subscribe to a Topic with the given QoS and invoke the callback whenever something is published to the Topic. Returns the QoS that was granted by the broker (lower or equal to the one requested).

The Topic may contain wildcars. The Topic passed to the callback is the fully expanded version where the message was actually published.

unsubscribe :: MQTT -> Topic -> IO () Source

Unsubscribe from the given Topic and remove any handlers.

publish :: MQTT -> QoS -> Bool -> Topic -> ByteString -> IO () Source

Publish a message to the given Topic at the requested QoS level. The payload can be any sequence of bytes, including none at all. The Bool parameter decides if the server should retain the message for future subscribers to the topic.

The Topic must not contain wildcards.

Sending and receiving Messages

send :: MQTT -> Message t -> IO () Source

Send a Message to the server.

addHandler :: SingI t => MQTT -> (Message t -> IO ()) -> IO Unique Source

Register a callback that gets invoked whenever a Message of the expected MsgType is received. Returns the ID of the handler which can be passed to removeHandler.

removeHandler :: MQTT -> Unique -> IO () Source

Remove the handler with the given ID.

awaitMsg :: SingI t => MQTT -> SMsgType t -> Maybe MsgID -> IO (Message t) Source

Block until a Message of the given type, optionally with the given MsgID, arrives.

Note this expects a singleton to guarantee the returned Message is of the MsgType that is being waited for. Singleton constructors are the MsgType constructors prefixed with a capital S, e.g. SPUBLISH.

awaitMsg' :: SingI t => MQTT -> Maybe MsgID -> IO (Message t) Source

A version of awaitMsg that infers the type of the Message that is expected.

Reexports