Skip to content

MQTT: topic rewrite on VerneMQ #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions mqtt/topic-rewrite-on-vernemq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Topic rewrite on VerneMQ

I want the MQTT client to subscribe to VerneMQ using a device ID based topic name, but to actually subscribe to a different topic.

For example, I want the MQTT client to being able to subscribe a topic named `/device/123` so it must only know its device ID (`123` in this example).

Since the device can chage over time I want the data to be published to the topic `/thing/456`.

VerneMQ has a nice feature called topic rewrite, available for both `SUBSCRIBE` and `PUBLISH` contro packets from the MQTT protocol.

The subscribe can be customized using the [auth_on_subscribe](https://docs.vernemq.com/plugindevelopment/subscribeflow#auth_on_subscribe-and-auth_on_subscribe_m5) hook.

The publish can be customized using the [auth_on_publish](https://docs.vernemq.com/v/master/plugin-development/publishflow#auth_on_publish-and-auth_on_publish_m5) hook.


```
┌──────────┐ ┌─────────────┐ ┌─────────────┐
│ client │ │ mqtt broker │ │ plugin │
└──────────┘ └─────────────┘ └─────────────┘
│ │ │
│ │ │
│ subscribe │ │
│ /device/123 │ │
│ ──────────────────────────▶ │ │
│ │ auth_on_su │
│ │ ──────bscribe───────▶ │
│ │ │
│ │ rewrite w/ │
│ │ ◀ ─ /thing/456─ ─ ─ ─ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │
```