/ht2mq

HTTP request to MQTT message bridge (relay)

Primary LanguageJavaScriptMIT LicenseMIT

HTTP 2 MQTT

Simple HTTP request to MQTT message bridge (relay).

Usage

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

HTTP GET

$ 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}}

HTTP POST

$ 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}}

HTTP * (Payload in header)

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.

Precedence

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:

  1. Header (x-payload)
  2. Query String (?payload_goes_here)
  3. Body (http request body)

Environment Variables

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).