美文网首页
mac端使用mosquitto部署mqtt服务

mac端使用mosquitto部署mqtt服务

作者: 谢二九 | 来源:发表于2022-12-06 09:54 被阅读0次

简介

mqtt是基于发布/订阅范式的消息协议,它的特点是轻量、简单、开放和易于实现。笔者从事多年IM通信的端侧开发,为了更深入的了解IM的全栈技术,期望搭建一个用于收发消息的能力。经过选择,最后选择了 Mosquitto 作为搭建服务的工具。

安装mosquitto

控制台直接输入 brew install mosquitto 。安装成功后会提示你修改配置文件(/opt/homebrew/etc/mosquitto/mosquitto.conf,注意:每个人安装的homebrew不太一样,看具体的安装成功的位置提示),以及提示你可以通过命令 /opt/homebrew/opt/mosquitto/sbin/mosquitto -c /opt/homebrew/etc/mosquitto/mosquitto.conf 启动一个临时服务。当然你可可以通过一下命令来启动/关闭服务:

停止服务 brew services stop mosquitto
启动服务 brew services start mosquitto
重启服务 brew services restart mosquitto

配置服务

用编辑器打开配置(/opt/homebrew/etc/mosquitto/mosquitto.conf),发现里面很多的配置项,具体配置用途可参考 官方文档 。我们为了快速验证,所以简单改以下两个配置:

listener 1883 -- 指定端口
allow_anonymous true -- 允许匿名访问

如果商业化,则需要设置访问密码,不做多述。

使用方式

  • 打开终端,启动服务:/opt/homebrew/opt/mosquitto/sbin/mosquitto -c /opt/homebrew/etc/mosquitto/mosquitto.conf
  • 打开新的终端,订阅消息:mosquitto_sub -t "test"
  • 打开新的终端,推送消息:mosquitto_pub -t "test" -m "123456"
    启动监听服务
推送消息 订阅消息

相关文章

网友评论

      本文标题:mac端使用mosquitto部署mqtt服务

      本文链接:https://www.haomeiwen.com/subject/ufywfdtx.html