In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Python to develop EMQ X MQTT server plug-in". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to use Python to develop EMQ X MQTT server plug-in" together!
EMQ X MQTT server provides a special multi-language plug-in emqx_extension_hook since v4.1. It now supports using other programming languages to handle hook events in EMQ X. Developers can use Python or Java to quickly develop their own plug-ins and extend them on the basis of official functions to meet their own business scenarios. For example:
Verify the login permission of a client: trigger the corresponding function when the client connects, and determine whether there is login permission by reading the database and comparing after obtaining the client information through parameters.
Record client online status and online history: trigger corresponding function when client status changes, obtain client information through parameters, rewrite client online status in database
Verify PUB/SUB operation permission of a client: trigger corresponding function when publishing/subscribing, obtain client information and current topic through parameters, and determine whether the client has corresponding operation permission
Process sessions and Message events to implement subscription relationships and message processing/storage: trigger corresponding functions when a message is published or status changes to obtain current client information, message status and message content, and forward them to Kafka or database for storage.
Note: Message hooks are supported only in Enterprise Edition.
Python and Java drivers are implemented based on Erlang/OTP-Port inter-process communication, which has very high throughput performance. This article takes Python extension as an example to introduce the use of EMQ X cross-language extension.
Python Extension Example Requirements
EMQ X requires Python 3.6 or above installed on the server
using step
Install Python SDK via pip
Adjust EMQ X configuration to ensure that relevant configuration items point to Python projects correctly
Introducing SDK to write code
Python Plugin Installation
Install SDK locally via pip command, ensure installation is done using pip3:
pip3 install emqx-extension-sdk modify configuration
Modify emqx-extension-hook plug-in configuration to use extensions correctly:
## Setup the supported drivers#### Value: python2 | python3 |javaexhook.drivers = python3## Search path for scripts/libraryexhook.drivers.python3.path = data/extension/hooks.py## Call timeout### Value: Duration##exhook.drivers.python3.call_timeout = 5s## Initial module name## Your filename or module nameexhook.drivers.python3.init_module = hooks Code
Create a new hooks.py file under emqx/data/extension directory and introduce SDK to write business logic. The example program is as follows:
## data/extension/hooks.pyfrom emqx_extension.hooks import EmqxHookSdk, hooks_handler from emqx_extension.types import EMQX_CLIENTINFO_PARSE_T, EMQX_MESSAGE_PARSE_T#Inheriting SDK HookSdk class CustomHook(EmqxHookSdk): #Register hooks with decorators @hooks_handler() def on_client_connect(self, conninfo: EMQX_CLIENTINFO_PARSE_T = None, props: dict = None, state: list = None): print(f'[Python SDK] [on_client_connect] {conninfo.clientid} connecte') @hooks_handler() def on_client_connected(self, clientinfo: EMQX_CLIENTINFO_PARSE_T, state: list = None): print( f'[Python SDK] [on_client_connected] {clientinfo.clientid} connected') @hooks_handler() def on_client_check_acl(self, clientinfo: EMQX_CLIENTINFO_PARSE_T, pubsub: str, topic: str, result: bool, state: tuple) -> bool: print( f'[Python SDK] [on_client_check_acl] {clientinfo.username} check ACL: {pubsub} {topic}') #ACL verification fails when username is empty if clientinfo.username == '': return False return True @hooks_handler() def on_client_authenticate(self, clientinfo: EMQX_CLIENTINFO_PARSE_T, authresult, state) -> bool: print( f'[Python SDK] [on_client_authenticate] {clientinfo.clientid} authenticate') # clientid is not empty, verification passes if clientinfo.clientid != '': return True return False # on_message_* Enterprise version only @hooks_handler() def on_message_publish(self, message: EMQX_MESSAGE_PARSE_T, state): print( f'[Python SDK] [on_message_publish] {message.topic} {message.payload}')emqx_hook = CustomHook(hook_module=f'{__name__}.emqx_hook')def init(): return emqx_hook.start()def deinit(): return Start
Start emqx_extension_hook plug-in, if the configuration error or code writing error will not start properly. After startup, try to establish MQTT connection and observe business operation.
./ bin/emqx_ctl plugins load emqx_extension_hook Thank you for reading, the above is "how to use Python to develop EMQ X MQTT server plug-in" content, after the study of this article, I believe everyone on how to use Python to develop EMQ X MQTT server plug-in this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.