go-http2mqtt is go daeamon (or go library http2mqtt) useful to talk to mqtt protocol throught REST API. it supports
- publish data to topic
- subscribe to topic receiving data via sse stream
You can also:
- configure partially the tool from API
It can be used to as a quick integration of mqtt language in a web application.
Install the dependencies and start the server.
$ go get
$ cd cmd/go-http2mqtt/
$ go build
to start deamon (note port is required in the host)
$ cd cmd/go-http2mqtt/
$ ./go-http2mqtt <host:port> <mqttbroker:port>
if broker requires credentials
$ cd cmd/go-http2mqtt/
$ ./go-http2mqtt <host:port> <mqttbroker:port> -k <username:password>
if you want to authenticate the API with user:password
$ cd cmd/go-http2mqtt/
$ ./go-http2mqtt <host:port> <mqttbroker:port> -u $user -p $password
go-http2mqtt exposes the following endpoints: (in case of port 8000 on localhost)
http://localhost:8000/ping
http://localhost:8000/publish
http://localhost:8000/subscribe
http://localhost:8000/broker
http://localhost:8000/streams
ws://localhost:8000/ws
This is the only always API NOT authenticated: it is a simple check to query a reply "pong"
$ curl -i -X GET -H "Content-Type: application/json" http://localhost:8000/ping
the json payload is:
{
"topic": "/topic/1",
"data": "this is a test",
"qos" : 0,
"retained" : "true"
}
"qos and "retained" fields are optionals
curl -u $user:$pass -i -X POST -H "Content-Type: application/json" -d '{"topic":"/topic/1" , "data": "this is a test"}' http://localhost:8000/publish
the json payload is:
{
"topic": "/topic/1",
"qos" : 0
}
"qos is optional
curl -u $user:$pass -i -X POST -H "Content-Type: application/json" -d '{"topic": "/topic/1" , "qos": 0}' http://localhost:8000/subscribe
To get info about subscriptions and other broker's stuff:
curl -u $user:$pass curl -i -X GET -H "Content-Type: application/json" http://localhost:8000/broker
{
"broker":"localhost:1883",
"connected":true,
"subscriptions":[{"topic":"/topic/1","qos":0}],
"user":null
}
sse stream to receive messages of the subscribed topics event:/topic/1 data:"eyJjb21tYW5kIjoicmVzZXQifQ=="
curl -u $user:$pass curl -i -X GET -H "Content-Type: application/json" http://localhost:8000/streams
websocket stream to receive messages of the subscribed topics (using websocat from https://github.com/vi/websocat)
websocat ws://localhost:8000/ws
Want to contribute? Great!
- Write Tests
- Add Swagger Doc
MIT
Free Software!