Home > Article > Backend Development > PHP and MQTT: Building an event-driven real-time data analysis system
PHP and MQTT: Building an event-driven real-time data analysis system
In today's digital era, real-time data analysis has become the key to corporate decision-making and business optimization. To achieve efficient real-time data analysis, a reliable and flexible system is required to collect, process and store data. In this article, we will introduce how to use PHP and MQTT (Message Queuing Telemetry Transport) to build an event-driven real-time data analysis system.
<?php require 'vendor/autoload.php'; use EclipseMosquittoClient as MosquittoClient; $client = new MosquittoClient(); $client->setCredentials('username', 'password'); // 如果需要认证,添加用户名和密码 $client->onConnect(function () use (&$client) { $client->publish('topic', 'Hello from PHP!', 0, false); $client->disconnect(); }); $client->onDisconnect(function () { echo "Disconnected from MQTT broker."; }); $client->connect('localhost', 1883, 60); $client->loopForever(); ?>
The above code first passes require 'vendor/autoload.php ';
Introduce the Paho MQTT client library, and then create a new client instance. Login authentication information can be set using the setCredentials
method. In the onConnect
event callback, you can use the publish
method to publish a message to the specified topic and then close the connection. Finally, use the connect
method to connect to the MQTT broker server and the loopForever
method to keep the connection alive.
To subscribe to a topic, you can use the following code:
<?php require 'vendor/autoload.php'; use EclipseMosquittoClient as MosquittoClient; $client = new MosquittoClient(); $client->setCredentials('username', 'password'); // 如果需要认证,添加用户名和密码 $client->onConnect(function () use (&$client) { $client->subscribe('topic', 0); }); $client->onMessage(function ($message) { echo "Received message: " . $message->payload . " "; }); $client->connect('localhost', 1883, 60); $client->loopForever(); ?>
The above code is similar to the publishing code. First, introduce the required libraries, create a client instance, and set the authentication information. In the onConnect
event callback, use the subscribe
method to subscribe to the specified topic. In the onMessage
event callback you can handle the received message. Finally, also use the connect
method to connect to the MQTT proxy server and keep the connection active.
<?php $servername = 'localhost'; $username = 'username'; $password = 'password'; $dbname = 'database'; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // 准备SQL语句 $sql = "INSERT INTO data (timestamp, value) VALUES ('" . time() . "', '10.5')"; // 执行SQL语句 if ($conn->query($sql) === TRUE) { echo "Data inserted successfully."; } else { echo "Error: " . $sql . "<br>" . $conn->error; } // 关闭连接 $conn->close(); ?>
The above code first creates a MySQL connection and checks whether the connection is successful. Then, prepare a SQL statement containing the data to be inserted, and execute it. Finally close the connection.
Conclusion:
In this article, we introduced how to use PHP and MQTT to build an event-driven real-time data analysis system. First, we installed and configured the MQTT proxy server, then used PHP to connect to the proxy server and publish or subscribe to messages on a specific topic. Finally, we wrote the data processing and storage logic code using PHP to demonstrate how to store data in a MySQL database. By using such a system, businesses can monitor and analyze data in real time to make timely decisions and optimize their business.
References:
Note: The above code is for reference only, please configure and modify it according to the actual situation.
The above is the detailed content of PHP and MQTT: Building an event-driven real-time data analysis system. For more information, please follow other related articles on the PHP Chinese website!