How to implement VerneMQ plugins using a HTTP/HTTPS interface
The VerneMQ Webhooks plugin provides an easy and flexible way to build powerful plugins for VerneMQ using web hooks. With VerneMQ Webhooks you are free to select the implementation language to match your technical requirements or the language in which you feel comfortable and productive in. You can use any modern language such as Python, Go, C#/.Net and indeed any language in which you can build something that can handle HTTP(s) requests.
The idea of VerneMQ Webhooks very simple: you can register an HTTP(s) endpoint with a VerneMQ plugin hook and whenever the hook (such as auth_on_register
) is called, the VerneMQ Webhooks plugin dispatches a HTTP post request to the registered endpoint. The HTTP post request contains a HTTP header like vernemq-hook: auth_on_register
and a JSON encoded payload. The endpoint then responds with code 200 on success and with a JSON encoded payload informing the VerneMQ Webhooks plugin which action to take (if any).
To enable webhooks make sure to set:
And then each webhook can be configured like this:
It is possible to have the webhooks plugin omit sending the payload for the auth_on_publish and auth_on_publish_m5 webhooks by setting the no_payload
config:
It is also possible to dynamically register webhooks at run-time:
See which endpoints are registered:
And finally deregistering an endpoint:
You might consider placing the endpoint implementation locally on each VerneMQ node such that each request can go over localhost without being subject to network issues.
In case your WebHooks backend requires HTTPS, you can configure the VerneMQ internal HTTP client to do so as well. There are various option you can set in the vernemq.conf
file:
Check the WebHooks Schema file for quick documentation on those options or to look up their configured defaults.
Each registered hook uses by default a connection pool containing maximally 100 connections. This can be changed by setting vmq_webhooks.pool_max_connections
to a different value. Similarly the vmq_webhooks.pool_timeout
configuration (value is in milliseconds) can be set to control how long an unused connection should stay in the connection pool before being closed and removed. The default value is 60000 (60 seconds).
VerneMQ webhooks support caching of the auth_on_register
, auth_on_publish
, auth_on_subscribe
, auth_on_register_m5
, auth_on_publish_m5
and auth_on_subscribe_m5
hooks.
This can be used to speed up authentication and authorization tremendously. All data passed to these hooks is used to look if the call is in the cache, except in the case of the auth_on_publish
and auth_on_publish_m5
where the payload is omitted.
To enable caching for an endpoint simply return the cache-control: max-age=AgeInSeconds
in the response headers to one of the mentioned hooks. If the call was successful (authentication granted), the request will be cached together with any modifiers, except for the payload
modifier in the auth_on_publish
hook.
Whenever a non-expired entry is looked up in the cache the endpoint will not be called and the modifiers of the cached entry will be returned, if any.
It is possible to inspect the cache using:
Cache entries are currently not actively disposed after expiry and will remain in memory.
All webhooks are called with method POST
. All hooks need to be answered with the HTTP code 200
to be considered successful. Any hook called that does not return the 200
code will be logged as an error as will any hook with an unparseable payload.
All hooks are called with the header vernemq-hook
which contains the name of the hook in question.
For detailed information about the hooks and when they are called, see the sections Session Lifecycle, Subscribe Flow and Publish Flow.
Note, when overriding a mountpoint or a client-id both have to be returned by the webhook implementation for it to have an effect.
All hooks, unless stated otherwise, respond with a JSON-encoded payload and a success code of 200. All hooks support responding with "ok", indicated that the request was successful.
Other possible responses are "next", meaning that the next callback should be tried.
Errors, e.g. authentication failures, are returned by a an "error" payload, either with the predefined "not_allowed"
or some other error text:
Header: vernemq-hook: auth_on_register
Webhook example payload:
Additionaly, to the standard "ok" response. It is also possible to override various client specific settings by returning an array of modifiers:
Note, the retry_interval
is in milli-seconds. It is possible to override many more settings, see the Session Lifecycle for more information.
Other possible responses are next and error (not_allowed).
Header: vernemq-hook: auth_on_subscribe
Webhook example payload:
An example where where the topics to subscribe have been rewritten looks like:
Note, you can also pass a qos
with value 128
which means it was either not possible or the client was not allowed to subscribe to that specific question.
Other possible responses are "next" and "error".
Header: vernemq-hook: auth_on_publish
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
A complex example where the publish topic, qos, payload and retain flag is rewritten looks like:
Other possible responses are "next" and "error".
Header: vernemq-hook: on_register
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_publish
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_subscribe
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_unsubscribe
Webhook example payload:
Example response:
Other possible responses are "next" and "error".
Header: vernemq-hook: on_deliver
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
Example response:
An other possible response is "next".
Header: vernemq-hook: on_offline_message
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_client_wakeup
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_client_offline
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_client_gone
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: auth_on_register_m5
Webhook example payload:
It is also possible to override various client specific settings by returning an array of modifiers:
Note, the retry_interval
is in milli-seconds. It is possible to override many more settings, see the Session Lifecycle for more information.
Other possible responses are "next" and "error".
Header vernemq-hook: on_auth_m5
Webhook example payload:
Note, as the authentication data is binary data it is base64 encoded.
A minimal response indicating the authentication was successful looks like:
If authentication were to continue for another round a reason code with value 24 (Continue Authentication) should be returned instead. See also the relevant section in the MQTT 5.0 specification.
Header: vernemq-hook: auth_on_subscribe_m5
Webhook example payload:
An example where where the topics to subscribe have been rewritten looks like:
Note, the forbidden/topic
has been rejected with the qos
value of 135 (Not authorized).
Other possible responses are "next" and "error".
Header: vernemq-hook: auth_on_publish_m5
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
A response where the publish topic has been rewritten:
Other possible responses are "next" and "error" (not_allowed).
Header: vernemq-hook: on_register_m5
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_publish_m5
Note, in the example below the payload is base64 encoded .
Webhook example payload:
The response should be an empty json object {}
.
Header: vernemq-hook: on_subscribe_m5
Webhook example payload:
Note, the qos value of 128
(Unspecified error) means the subscription was rejected.
The response should be an empty json object {}
.
Header: vernemq-hook: on_unsubscribe_m5
Webhook example payload:
Example response:
It supports the standard "OK" response, as well "next".
Header: vernemq-hook: on_deliver_m5
Note, in the example below the payload is not base64 encoded which is not the default.
Webhook example payload:
It supports the standard "OK" response, as well "next" and "error".
Below is a very simple example of an endpoint implemented in Python. It uses the web
and json
modules and implements handlers for six different hooks: auth_on_register
, auth_on_publish
, auth_on_subscribe
, auth_on_register_m5
, auth_on_publish_m5
and auth_on_subscribe_m5
.
The auth_on_register
hook only restricts access only to the user with username joe
and password secret
. It also shows how to cache the result. The auth_on_subscribe
and auth_on_publish
hooks allow any subscription or publish to continue as is. These last two hooks are needed as the default policy is deny
.
The following configuration can be used for testing the Python example.