Copyright | Lukas Braun 2014 |
---|---|
License | GPL-3 |
Maintainer | koomi+mqtt@hackerspace-bamberg.de |
Safe Haskell | None |
Language | Haskell2010 |
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!"
- connect :: MQTTConfig -> IO (Maybe MQTT)
- data MQTT
- disconnect :: MQTT -> IO ()
- reconnect :: MQTT -> Int -> IO ()
- onReconnect :: MQTT -> IO () -> IO ()
- resubscribe :: MQTT -> IO [QoS]
- data MQTTConfig
- defaultConfig :: MQTTConfig
- cHost :: MQTTConfig -> HostName
- cPort :: MQTTConfig -> PortNumber
- cClean :: MQTTConfig -> Bool
- cWill :: MQTTConfig -> Maybe Will
- cUsername :: MQTTConfig -> Maybe Text
- cPassword :: MQTTConfig -> Maybe Text
- cKeepAlive :: MQTTConfig -> Maybe Int
- cClientID :: MQTTConfig -> Text
- cConnectTimeout :: MQTTConfig -> Maybe Int
- cReconnPeriod :: MQTTConfig -> Maybe Int
- cLogger :: MQTTConfig -> Logger
- subscribe :: MQTT -> QoS -> Topic -> (Topic -> ByteString -> IO ()) -> IO QoS
- unsubscribe :: MQTT -> Topic -> IO ()
- publish :: MQTT -> QoS -> Bool -> Topic -> ByteString -> IO ()
- send :: MQTT -> Message t -> IO ()
- addHandler :: SingI t => MQTT -> (Message t -> IO ()) -> IO Unique
- removeHandler :: MQTT -> Unique -> IO ()
- awaitMsg :: SingI t => MQTT -> SMsgType t -> Maybe MsgID -> IO (Message t)
- awaitMsg' :: SingI t => MQTT -> Maybe MsgID -> IO (Message t)
- module Network.MQTT.Types
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.
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?
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.
Sending and receiving Message
s
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.
Reexports
module Network.MQTT.Types