In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the use of MQTT in the PHP project". In the daily operation, I believe that many people have doubts about the use of MQTT in the PHP project. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about the use of MQTT in the PHP project. Next, please follow the editor to study!
MQTT client Library selection
This article chooses php-mqtt/client, the client library with the highest composer load, and more PHP-MQTT client libraries can be seen in Packagist-Search MQTT.
For more documentation on the use of php-mqtt/client, see Packagist php-mqtt/client.
MQTT communication belongs to the network communication scenario outside the HTTP system. Due to the limitation of PHP characteristics, the use of Swoole/Workerman and other extensions specially built for network communication in the PHP system can bring a better experience. Its use will not be discussed in detail in this article. The relevant MQTT client libraries are as follows:
Workerman/mqtt:Asynchronous MQTT client for PHP based on workerman.
Simps/mqtt:MQTT Protocol Analysis and Coroutine Client for PHP.
Project initialization confirm PHP version
This project uses 7.4.21 for development testing, and readers can confirm the version of PHP with the following command.
Php--versionPHP 7.4.21 (cli) (built: Jul 12 2021 11:52:30) (NTS) Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies installs php-mqtt/client client using Composer
Composer is a dependency management tool for PHP that manages all the dependencies you need for your PHP project.
Composer require php-mqtt/clientPHP MQTT uses to connect to a MQTT server
This article will use the free public MQTT server provided by EMQ X, which is based on EMQ X's MQTT Internet of things cloud platform. The server access information is as follows:
Broker: broker-cn.emqx.io
TCP Port: 1883
SSL/TLS Port: 8883
Import composer autoload file and php-mqtt/clientrequire ('vendor/autoload.php'); use\ PhpMqtt\ Client\ MqttClient; set MQTT Broker connection parameters
Set the MQTT Broker connection address, port and topic, and we call the PHP rand function to randomly generate the MQTT client id.
$server = 'broker-cn.emqx.io';$port = 1883 MQTT clientId = rand (5,15); $username =' emqx_user';$password = null;$clean_session = false; write MQTT connection function
Use the above parameters to connect, and set the connection parameters through ConnectionSettings, such as
$connectionSettings = new ConnectionSettings (); $connectionSettings-> setUsername ($username)-> setPassword (null)-> setKeepAliveInterval (60) / / LastWill setting-> setLastWillTopic ('emqx/test/last-will')-> setLastWillMessage (' client disconnect')-> setLastWillQualityOfService (1); copy the code subscription message
Write code to subscribe to an emqx/test topic and configure a callback function for the subscription to process the received messages. Here we print out the topics and messages received by the subscription:
/ / subscribe to $mqtt- > subscribe ('emqx/test', function ($topic, $message) {printf ("Received message on topic [% s]:% s\ n", $topic, $message);}, 0); publish messages
Construct a payload and call the publish function to publish messages to the emqx/test topic. After the publication is completed, the client needs to enter the polling state to process the incoming messages and resend the queue:
For ($I = 0; $I
< 10; $i++) { $payload = array( 'protocol' =>'tcp',' date' = > date ('Y-m-d Hlav idate''), 'url' = >' https://github.com/emqx/MQTT-Client-Examples'); $mqtt- > publish (/ / topic 'emqx/test', / / payload json_encode ($payload), / / qos 0, / / retain true); printf ("msg $I send\ n"); sleep (1) } / / client polling to process incoming messages and resend queues $mqtt- > loop (true); complete code
Server connection, message publishing and receiving code.
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.