# WebHook

# Configuration item

The webhook configuration file is located in: etc/nanomq_web_hook.conf, the detailed description of configuration items can be found in Configuration item.

Enable Webhook

web.hook.enable=true
1

# Trigger rule

Trigger rules can be configured in etc/nanomq_web_hook.conf. The configuration format is as follows:

## Format example
web.hook.rule.<Event>.<Number>=<Rule>

## Example
web.hook.rule.message.publish.1={"action": "on_message_publish", "topic": "a/b/c"}
web.hook.rule.message.publish.2={"action": "on_message_publish", "topic": "foo/#"}
1
2
3
4
5
6

# Trigger event

The following events are currently supported:

NameDescriptionExecution timing
client.connackIssue connection acknowledgeWhen the server is ready to send connack packet
client.disconnecteddisconnectedWhen the client connection layer is about to close
message.publishmessage publishedBefore the server rpublishes (routes) the message

# Number

Multiple trigger rules can be configured for the same event, and events with the same configuration should be incremented in sequence.

# Rule

The trigger rule's 'value is a JSON string, and the available Keys are:

  • action: string, taking a fixed value
  • topic: a string, indicating a topic filter, the operation topic can only trigger the forwarding of the event if it matches the topic

For example, we only forward messages matching the topics of a/b/c and foo/# to the web server, and the configuration should be:

web.hook.rule.message.publish.1 = {"action": "on_message_publish", "topic": "a/b/c"}
web.hook.rule.message.publish.2 = {"action": "on_message_publish", "topic": "foo/#"}
1
2

In this way, Webhook will only forward messages matching the topics of a/b/c and foo/#, such as foo/bar, etc., instead of forwarding a/b/d or fo/bar

# Webhook event parameters

When the event is triggered, Webhook will group each event into an HTTP request and sent it to the web server configured by url according to the configuration. The request format is:

URL: <url>      # From the url field in the configuration
Method: POST    # Fixed as POST method

Body: <JSON>    # Body is a JSON format string
1
2
3
4

For different events, the content of the request body is different. The following table lists the parameters of the body in each event:

client.connack

KeyTypeDescription
actionstringevent name
fixed at: "client_connack"
clientidstringclient ClientId
usernamestringclient Username, When not existed, the value is "undefined"
keepaliveintegerHeartbeat keepalive time applied by client
proto_verintegerProtocol version number (3 | 4 | 5)
conn_ackstring"success" means success, other means failure

client.disconnected

KeyTypeDescription
actionstringevent name
fixed at: "client_disconnected"
clientidstringclient ClientId
usernamestringclient Username, When not existed, the value is "undefined"
reasonstringerror reason

message.publish

KeyTypeDescription
actionstringevent name
fixed at: "message_publish"
from_client_idstringPublisher's ClientId
from_usernamestringPublisher's Username, When not existed, the value is "undefined"
topicstringUnsubscribed topic
qosenumQoS level, and the optional value is 0 1 2
retainboolWhether it is a Retain message
payloadstringMessage Payload
tsintegerTimestamp (second)