使用PHP在Laravel上进行mqtt [英] mqtt on Laravel with PHP
问题描述
我在树莓派和Ubuntu上使用MQTT.我使用终端作为开始订阅者和发布者,并且效果很好.但是现在我想创建一个具有此功能的网站(使用laravel).我用谷歌搜索,但没有发现任何有趣的东西.有可能这样做吗?
I'm using the MQTT on my raspberry and on my Ubuntu. I use terminal for start subscriber and publisher and it works so good. But now I want to create a website (using laravel) with this features. I googled it, but I don't find anything interesting. Is it possible to do and how?
推荐答案
与HTTP不同,MQTT通常用于打开应用程序进程和MQTT服务器之间的长期连接.
Unlike with HTTP, MQTT is typically used by opening a long-running connection between an application process and a MQTT server.
虽然完全有可能打开连接,发布一条消息,然后再次关闭套接字,但订阅消息流实际上并没有用.
While it is perfectly possible to open a connection, publish a single message and then close the socket again, it doesn't really work for subscribing to a stream of messages.
PHP的典型操作模式是启动一个进程,等待HTTP连接,处理请求,然后启动一个新进程.这与具有长时间运行过程的典型MQTT模式不太匹配.
PHP's typically mode of operation is to start a process, wait for an HTTP connection, handle the request and then start a new process. This doesn't fit well with the typical MQTT mode of having a long-running process.
使用PHP内置HTTP服务器的人们开始对此有所改变.我不确定Laravel是否提供一种机制来在处理请求之间保持变量/TCP连接到位.
This had started to change with people using the PHP built-in HTTP server. I am not sure if Laravel provides a mechanism to keep variables/TCP connections in place between handling requests.
或者,可以在长时间运行的CLI PHP脚本中订阅MQTT主题,并将其持久化在本地(例如数据库,KV存储,内存缓存,redis),然后从数据存储中提供数据.
Alternatively it is possible to subscribe to a MQTT topic in a long-running CLI PHP script and persisting it locally (for example a database, KV store, memcache, redis) and then serving the data from the datastore.
这在很大程度上取决于您要实现的目标.
It depends a lot on what you are trying to achieve.
这篇关于使用PHP在Laravel上进行mqtt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!