欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EMQX中如何使用hooks功能 EMQXhooksweb-hookseventplugins 

程序员文章站 2022-04-27 11:04:08
...
【EMQX开始hook功能】
The EMQ broker implements a simple but powerful hooks mechanism to help users develop plugin.
The broker would run the hooks when a client is connected/disconnected, a topic is subscribed/unsubscribed or a MQTT message is published/delivered/acked

EMQX实现了简单并强大的hooks机制,帮助用户开发插件。
Broker在以下情况下运行hook:客户端建立连接、客户端断开连接、订阅主题、取消订阅主题、消息发布、消息分发、消息应答

1> EMQX提供两种hook,luahook和webhook
2> hook属于插件功能,开启hook有以下方式:

A> 通过# ./emqx_ctl plugins子命令进行配置
   =># ./emqx_ctl plugins load emqx_web_hook
   =># ./emqx_ctl plugins unload emqx_web_hook

B> 通过emqx webconsole在界面进行配置

3> webhook默认支持以下action
//////////begin///////////
web.hook.rule.client.connected.1     = {"action": "on_client_connected"}
web.hook.rule.client.disconnected.1  = {"action": "on_client_disconnected"}
web.hook.rule.client.subscribe.1     = {"action": "on_client_subscribe"}
web.hook.rule.client.unsubscribe.1   = {"action": "on_client_unsubscribe"}
web.hook.rule.session.created.1      = {"action": "on_session_created"}
web.hook.rule.session.subscribed.1   = {"action": "on_session_subscribed"}
web.hook.rule.session.unsubscribed.1 = {"action": "on_session_unsubscribed"}
web.hook.rule.session.terminated.1   = {"action": "on_session_terminated"}
web.hook.rule.message.publish.1      = {"action": "on_message_publish"}
web.hook.rule.message.deliver.1    = {"action": "on_message_deliver"}
web.hook.rule.message.acked.1        = {"action": "on_message_acked"}
//////////end////////////
注意:实际使用时,可针对需要的hook action进行配置,仅保留需要的ation即可,
比如:需要检测客户端是否断开的行为,可以只保留【on_client_disconnected】这个action。

4> webhook默认发起请求的方式如下:
0> uri=>可根据实际使用场景进行配置
1> method=POST
2> content-type=application/json
3> body data如下:
//////////////begin////////
{"action":"session_terminated","client_id":"ac110a0f98e25d9d8b75","reason":"normal"}
//////////////end//////////
注意:可通过client_id来关联到实际业务场景中;

参考:https://github.com/emqx/emqx-web-hook