emq插件demoemx插件

emq插件demo emx插件

EMQ X 是一个分布式消息中间件,用于构建高可用、高性能的物联网和实时应用。EMQ X 提供了多种插件,可以帮助开发者实现各种功能,例如消息队列、事件总线、订阅/发布模式等。以下是一个简单的 EMQ X 插件示例:

创建一个名为 emqx_plugin 的插件项目。在命令行中执行以下命令:
emsdk create plugin emqx_plugin
进入插件项目目录,创建一个新的 Python 文件 emqx_plugin_demo.py
cd emqx_plugin/emqx_plugin_demotouch emqx_plugin_demo.py
编辑 emqx_plugin_demo.py 文件,添加以下代码:
import osfrom pyamqp import AMQPConnection, Channeldef connect(host, port):    connection = AMQPConnection(host, port)    return connectiondef publish(connection, channel, message):    channel.basic_publish(exchange="logs", routing_key="hello", body=message)def consume(connection, channel):    while True:        message = channel.basic_consume(queue="logs", on_message_callback=lambda x: print(f"Received message: {x.content}"))        if not message:            break        channel.basic_ack(delivery_tag=message.delivery_tag)def main():    host = "localhost"    port = 5672    connection = connect(host, port)    channel = connection.channel()    channel.queue_declare(queue="logs")    channel.basic_qos(prefetch_count=1)    channel.basic_consume(queue="logs", on_message_callback=publish, auto_ack=True)if __name__ == "__main__":    main()
运行插件项目,启动 EMQ X 服务器:
emsdk start emqx_plugin_demo
在浏览器中访问 http://localhost:5672/emqx/websocket,查看日志消息。

na.png

本网站文章未经允许禁止转载,合作/权益/投稿 请联系平台管理员 Email:epebiz@outlook.com