Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timbuchwaldt/exmqttc
Elixir wrapper for the emqttc library
https://github.com/timbuchwaldt/exmqttc
elixir emqttc mqtt mqtt-client
Last synced: about 2 months ago
JSON representation
Elixir wrapper for the emqttc library
- Host: GitHub
- URL: https://github.com/timbuchwaldt/exmqttc
- Owner: timbuchwaldt
- License: mit
- Created: 2017-05-07T19:32:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-02T21:11:22.000Z (about 6 years ago)
- Last Synced: 2024-09-18T14:10:40.295Z (3 months ago)
- Topics: elixir, emqttc, mqtt, mqtt-client
- Language: Elixir
- Size: 56.6 KB
- Stars: 23
- Watchers: 3
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Exmqttc [![Deps Status](https://beta.hexfaktor.org/badge/all/github/timbuchwaldt/exmqttc.svg)](https://beta.hexfaktor.org/github/timbuchwaldt/exmqttc) [![Build Status](https://travis-ci.org/timbuchwaldt/exmqttc.svg?branch=master)](https://travis-ci.org/timbuchwaldt/exmqttc) [![Inline docs](http://inch-ci.org/github/timbuchwaldt/exmqttc.svg?branch=master)](http://inch-ci.org/github/timbuchwaldt/exmqttc)
Elixir wrapper for the emqttc library.
emqttc must currently be installed manually as it is not available on hex (yet).
## Installation
The package can be installed by adding `exmqttc` and `emqttc` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:exmqttc, "~> 0.5"}, {:emqttc, github: "emqtt/emqttc"}]
end
```## Usage
Create a callback module:
```elixir
defmodule MyClient do
require Logger
use Exmqttc.Callbackdef init(_params) do
{:ok, []}
enddef handle_connect(state) do
Logger.debug "Connected"
{:ok, state}
enddef handle_disconnect(state) do
Logger.debug "Disconnected"
{:ok, state}
enddef handle_publish(topic, payload, state) do
Logger.debug "Message received on topic #{topic} with payload #{payload}"
{:ok, state}
end
end
```You can keep your own state and return it just like with `:gen_server`.
Start the MQTT connection process by calling `start_link/3`:
```elixir
{:ok, pid} = Exmqttc.start_link(MyClient, [], [host: '127.0.0.1'], params)
```
The third argument is a list of emqtt connection options, supporting the following options- `host`: Connection host, charlist, default: `'localhost'`
- `port`: Connection port, integer, default 1883
- `client_id`: Binary ID for client, automatically set to UUID if not specified
- `clean_sess`: MQTT cleanSession flag. `true` disables persistent sessions on the server
- `keepalive`: Keepalive timer, integer
- `username`: Login username, binary
- `password`: Login password, binary
- `will`: Last will, keywordlist, sample: `[qos: 1, retain: false, topic: "WillTopic", payload: "I died"]`
- `connack_timeout`: Timeout for connack package, integer, default 60
- `puback_timeout`: Timeout for puback package, integer, default 8
- `suback_timeout`: Timeout for suback package, integer, default 4
- `ssl`: List of ssl options
- `auto_resub`: Automatically resubscribe to topics, boolean, default: `false`
- `reconnect`: Automatically reconnect on lost connection, integer (), default `false`Params are passed to the init function of your callback module.
You can publish messages to the given PID:
```elixir
Exmqttc.publish(pid, "test", "foo")
````publish/4` also supports passing in QOS and retain options:
```elixir
Exmqttc.publish(pid, "test", "foo", qos: 2, retain: true)
```Further docs can be found at [https://hexdocs.pm/exmqttc](https://hexdocs.pm/exmqttc).