SimpleMqtt (simple_mqtt v0.1.0) View Source
The SimpleMqtt is a basic, single node pub-sub implementation where publishers and subscribers use topics and topics filters compatible with MQTT.
It cannot replace a real MQTT broker, but can be used in a simple IoT device, with multiple local sensors and actuators that have to communicate with each other.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Publishes message to the given topic.
Starts new Simple MQTT server and links it to the current process
Subscribes the current process to the given list of topics. Each item in the list must be a valid MQTT filter.
Unsubscribes the current process from the given list of topics.
Link to this section Types
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Specs
Publishes message to the given topic.
Examples
{:ok, pid} = SimpleMqtt.start_link()
:ok = SimpleMqtt.publish(pid, "things/sensor_1/temperature", "34.5")
Starts new Simple MQTT server and links it to the current process
Specs
subscribe(pid(), [topic_filter()]) :: :ok
Subscribes the current process to the given list of topics. Each item in the list must be a valid MQTT filter.
Examples
In the following example, the current process subscribes to two topic filters:
{:ok, pid} = SimpleMqtt.start_link()
:ok = SimpleMqtt.subscribe(pid, ["things/sensor_1/+", "things/sensor_2/+"])
If the process needs to monitor one more topic filter, it can call subscribe
again. After this call, the current process
will be subscribed to three topic filters.
:ok = SimpleMqtt.subscribe(pid, ["things/sensor_3/+"])
Specs
unsubscribe(pid(), [topic_filter()] | :all) :: :ok
Unsubscribes the current process from the given list of topics.
Examples
In the following example, the current process starts the Simple MQTT server, subscribes to two topic filters, and then unsubscribes from the second one. It will still receive messages published to a topic that matches the first filter.
{:ok, pid} = SimpleMqtt.start_link()
:ok = SimpleMqtt.subscribe(pid, ["things/sensor_1/+", "things/sensor_2/+"])
:ok = SimpleMqtt.unsubscribe(pid, ["things/sensor_2/+"])
In the second example, the current process unsubscribes from all topics. It will no longer receive any messages.
{:ok, pid} = SimpleMqtt.start_link()
:ok = SimpleMqtt.subscribe(pid, ["things/sensor_1/+", "things/sensor_2/+"])
:ok = SimpleMqtt.unsubscribe(pid, :all)