Skip to main content
Version: 2.12

mqtt-proxy

Summary#

Name#

The plugin mqtt-proxy only works in stream model, it help you to dynamic load balance by client_id of MQTT.

And this plugin both support MQTT protocol 3.1.* and 5.0.

Attributes#

NameTypeRequirementDefaultValidDescription
protocol_namestringrequiredName of protocol, should be MQTT in normal.
protocol_levelintegerrequiredLevel of protocol, it should be 4 for MQTT 3.1.*. it should be 5 for MQTT 5.0.
upstreamobjectdeprecatedUse separate upstream in the route instead.
upstream.hoststringrequiredthe IP or host of upstream, will forward current request to.
upstream.ipstringdeprecatedUse "host" instead. IP address of upstream, will forward current request to.
upstream.portnumberrequiredPort of upstream, will forward current request to.

How To Enable#

To enable this plugin, we need to enable the stream_proxy configuration in conf/config.yaml first. For example, the following configuration represents listening on the 9100 TCP port.

    ...
router:
http: 'radixtree_uri'
ssl: 'radixtree_sni'
stream_proxy: # TCP/UDP proxy
only: false # needed if HTTP and Stream Proxy should be enabled
tcp: # TCP proxy port list
- 9100
dns_resolver:
...

Then send the MQTT request to port 9100.

Creates a stream route, and enable plugin mqtt-proxy.

curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
}]
}
}'

In case Docker is used in combination with MacOS host.docker.internal is the right parameter for host.

This plugin exposes a variable mqtt_client_id, and we can use it to load balance via the client id. For example:

curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4
}
},
"upstream": {
"type": "chash",
"key": "mqtt_client_id",
"nodes": [
{
"host": "127.0.0.1",
"port": 1995,
"weight": 1
},
{
"host": "127.0.0.2",
"port": 1995,
"weight": 1
}
]
}
}'

MQTT connections with different client ID will be forwarded to different node via the consistent hash algorithm. If the client ID is missing, we will balance via client IP instead.

Delete Plugin#

$ curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X DELETE

The mqtt-proxy plugin has been deleted now.