Simple HTTP request to MQTT message bridge (relay).
This package is available on docker hub as hertzg/ht2mq. Project was targeted towards being run inside docker, but you can also start it without.
Lets assume you started the service and have the accessible at http://ht2mq-bridge.svc.local
.
$ docker run -p 80:8080 -e MQTT_BROKER=mqtt://mqtt.svc.local hertzg/ht2mq:latest
$ curl -X GET http://ht2mq-bridge.svc.local/my/sample/topic?payload_goes_here_directly
{"topic":"my/sample/topic","payload":"payload_goes_here_directly","result":{"success":true}}
$ curl -X POST http://ht2mq-bridge.svc.local/my/sample/topic -d payload_goes_here_directly
{"topic":"my/sample/topic","payload":"payload_goes_here_directly","result":{"success":true}}
curl -X POST http://ht2mq-bridge.svc.local/my/sample/topic -H "X-Payload: payload_goes_here_directly"
{"topic":"my/sample/topic","payload":"payload_goes_here_directly","result":{"success":true}}
Note: The response DOES NOT contian the prefix, but it will be added when actual publish happens.
In cases where requests match multiple or all usecases (eg: sending a post request with payload in query string, header and body) the payload that will be forwarded to mqtt is baed on the following precedence:
- Header (
x-payload
) - Query String (
?payload_goes_here
) - Body (http request body)
VARIABLE | Default | Description |
---|---|---|
MQTT_BROKER |
'mqtt://host.docker.internal' |
Connects to the broker specified by the given url The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp', 'tls', 'ws', 'wss'. |
TOPIC_PREFIX |
'ht2mq/' |
Prefix to use for all topics generated by the bridge. To disable use an empty string '' . |
HTTP_PAYLOAD_HEADER |
'x-payload' |
Header to use for payload instead of pathname (optional). |
HTTP_PORT |
8080 |
Port to listen for HTTP connections (currently setup to match the Dockerfile EXPOSE ). |
MQTT_CLIENT_ID |
null |
MQTT Client ID (optional). |
MQTT_USERNAME |
null |
MQTT Username (optional). |
MQTT_PASSWORD |
null |
MQTT Password (optional). |
MQTT_RETAIN |
false |
Set true to retain messages in MQTT (optional). |