Skip to content

freedreamer82/go-http2mqtt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-http2mqtt

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.

Installation of the daemon

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

Endpoints

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
localhost:8000/ping {GET}

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
localhost:8000/publish {POST}

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
localhost:8000/subscribe {POST}

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
localhost:8000/broker{GET}

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
}
localhost:8000/streams{GET}

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
localhost:8000/ws{GET}

websocket stream to receive messages of the subscribed topics (using websocat from https://github.com/vi/websocat)

websocat ws://localhost:8000/ws

Development

Want to contribute? Great!

Todos

  • Write Tests
  • Add Swagger Doc

License

MIT

Free Software!