Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync blogs 2024-12-24-11-03-18 #1457

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Get to know the preferred protocol in IoT from beginner to master.
## [MQTT Programming](https://www.emqx.com/en/blog/category/mqtt-programming)
Best practice of MQTT in various clients.

- [How to Use MQTT with PHP: A Guide for Real-Time Messaging](https://www.emqx.com/en/blog/how-to-use-mqtt-in-php) ([Edit](https://github.com/emqx/blog/blob/main/en/202411/how-to-use-mqtt-in-php.md))
- [A Guide on Collecting and Reporting Soil Moisture with ESP32 and Sensor through MQTT](https://www.emqx.com/en/blog/hands-on-guide-on-esp32) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/hands-on-guide-on-esp32.md))
- [MQTT on ESP32: A Beginner's Guide](https://www.emqx.com/en/blog/esp32-connects-to-the-free-public-mqtt-broker) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/esp32-connects-to-the-free-public-mqtt-broker.md))
- [MQTT in Python with Paho Client: Beginner's Guide 2024](https://www.emqx.com/en/blog/how-to-use-mqtt-in-python) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/how-to-use-mqtt-in-python.md))
Expand All @@ -137,7 +138,6 @@ Best practice of MQTT in various clients.
- [How to Use MQTT in The Angular Project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-angular) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/how-to-use-mqtt-in-angular.md))
- [How to Use MQTT in The React Native Project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-react-native) ([Edit](https://github.com/emqx/blog/blob/main/en/202206/how-to-use-mqtt-in-react-native.md))
- [How to use MQTT in Flask](https://www.emqx.com/en/blog/how-to-use-mqtt-in-flask) ([Edit](https://github.com/emqx/blog/blob/main/en/202205/how-to-use-mqtt-in-flask.md))
- [How to use MQTT in PHP](https://www.emqx.com/en/blog/how-to-use-mqtt-in-php) ([Edit](https://github.com/emqx/blog/blob/main/en/202108/how-to-use-mqtt-in-php.md))
- [Python MQTT Asynchronous Framework - HBMQTT](https://www.emqx.com/en/blog/python-async-mqtt-client-hbmqtt) ([Edit](https://github.com/emqx/blog/blob/main/en/202104/python-async-mqtt-client-hbmqtt.md))
- [How to use MQTT in the Electron project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-electron) ([Edit](https://github.com/emqx/blog/blob/main/en/202010/how-to-use-mqtt-in-electron.md))
- [How to use MQTT in the Vue project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-vue) ([Edit](https://github.com/emqx/blog/blob/main/en/202010/how-to-use-mqtt-in-vue.md))
Expand Down Expand Up @@ -327,7 +327,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit
- [Understanding AWS IoT Core: Features, Use Cases & Quick Tutorial](https://www.emqx.com/en/blog/understanding-aws-iot-core) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/understanding-aws-iot-core.md))
- [IoT in the Cloud: 8 Key Benefits and How to Get Started](https://www.emqx.com/en/blog/iot-in-the-cloud-8-key-benefits-and-how-to-get-started) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/iot-in-the-cloud-8-key-benefits-and-how-to-get-started.md))
- [EMQX Cloud BYOC Now Available on AWS: Seamlessly Extend Your MQTT Infrastructure](https://www.emqx.com/en/blog/emqx-cloud-byoc-is-now-available-on-aws) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/emqx-cloud-byoc-is-now-available-on-aws.md))
- [Get Started with EMQX Cloud on Azure Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-azure-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/get-started-with-emqx-cloud-on-azure-marketplace.md))
- [Get Started with EMQX Platform on Azure Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-azure-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/get-started-with-emqx-cloud-on-azure-marketplace.md))
- [Connecting to Serverless MQTT Broker using MQTTX: A Quick Guide](https://www.emqx.com/en/blog/how-to-use-mqttx-for-serverless-mqtt-broker-test) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/how-to-use-mqttx-for-serverless-mqtt-broker-test.md))
- [EMQX Cloud Extends its Reach: Introducing New Regions for Seamless MQTT Connectivity](https://www.emqx.com/en/blog/emqx-cloud-extends-its-reach-introducing-new-regions-for-seamless-mqtt-connectivity) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/emqx-cloud-extends-its-reach-introducing-new-regions-for-seamless-mqtt-connectivity.md))
- [EMQX Cloud BYOC is Now Available on Google Cloud!](https://www.emqx.com/en/blog/emqx-cloud-byoc-is-now-available-on-google-cloud) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/emqx-cloud-byoc-is-now-available-on-google-cloud.md))
Expand All @@ -341,7 +341,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit
- [Expanding Observability: Enhanced MQTT Monitor Metrics in EMQX Cloud's Latest Release](https://www.emqx.com/en/blog/enhanced-mqtt-monitor-metrics-in-emqx-cloud-s-latest-release) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/enhanced-mqtt-monitor-metrics-in-emqx-cloud-s-latest-release.md))
- [Connecting to Serverless MQTT Broker in Node.js](https://www.emqx.com/en/blog/connecting-to-serverless-mqtt-broker-in-nodejs) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/connecting-to-serverless-mqtt-broker-in-nodejs.md))
- [How BYOC MQTT Service Ensures IoT Data Security and Compliance for Your Business](https://www.emqx.com/en/blog/how-byoc-mqtt-service-ensures-iot-data-security-and-compliance-for-your-business) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/how-byoc-mqtt-service-ensures-iot-data-security-and-compliance-for-your-business.md))
- [Get Started with EMQX Cloud on Google Cloud Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-gcp-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/get-started-with-emqx-cloud-on-gcp-marketplace.md))
- [Get Started with EMQX Platform on Google Cloud Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-gcp-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/get-started-with-emqx-cloud-on-gcp-marketplace.md))
- [Optimize Your MQTT Server with EMQX Cloud BYOC's Expert O&M Service](https://www.emqx.com/en/blog/optimize-your-mqtt-server-with-emqx-cloud-byoc-s-expert-o-and-m-service) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/optimize-your-mqtt-server-with-emqx-cloud-byoc-s-expert-o-and-m-service.md))
- [Exploring the Data Privacy-First Architecture of EMQX Cloud BYOC](https://www.emqx.com/en/blog/exploring-the-data-privacy-first-architecture-of-emqx-cloud-byoc) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/exploring-the-data-privacy-first-architecture-of-emqx-cloud-byoc.md))
- [A Comprehensive Guide to Serverless MQTT Service | EMQX Cloud](https://www.emqx.com/en/blog/a-comprehensive-guide-to-serverless-mqtt-service) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/a-comprehensive-guide-to-serverless-mqtt-service.md))
Expand All @@ -368,7 +368,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit
- [Save MQTT Data from EMQX Cloud on GCP to Timescale Cloud through the public network ](https://www.emqx.com/en/blog/save-mqtt-data-from-emqx-cloud-on-gcp-to-timescale-cloud-through-the-public-network) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/save-mqtt-data-from-emqx-cloud-on-gcp-to-timescale-cloud-through-the-public-network.md))
- [Optimization of EMQX 5.0: The Throughput Performance of ExProto Improves | Newsletter 202209](https://www.emqx.com/en/blog/emqx-newsletter-202209) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/emqx-newsletter-202209.md))
- [EMQX Cloud Update: Comprehensive Upgrade of Billing System](https://www.emqx.com/en/blog/emqx-cloud-update-comprehensive-upgrade-of-billing-system) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/emqx-cloud-update-comprehensive-upgrade-of-billing-system.md))
- [Get Started with EMQX Cloud on the AWS Marketplace with pay as you go](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go.md))
- [Get Started with EMQX Platform on the AWS Marketplace with Pay As You Go](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go.md))
- [Bridge MQTT Data from EMQX Cloud to Confluent Cloud on GCP](https://www.emqx.com/en/blog/bridge-mqtt-data-from-emqx-cloud-to-confluent-cloud-on-gcp) ([Edit](https://github.com/emqx/blog/blob/main/en/202209/bridge-mqtt-data-from-emqx-cloud-to-confluent-cloud-on-gcp.md))
- [Getting Data from EMQX Cloud with InfluxDB Native Collector](https://www.emqx.com/en/blog/getting-data-from-emqx-cloud-with-influxdb-native-collector) ([Edit](https://github.com/emqx/blog/blob/main/en/202208/getting-data-from-emqx-cloud-with-influxdb-native-collector.md))
- [EMQX Cloud update: Data integration support HStreamDB and Tablestore](https://www.emqx.com/en/blog/data-integration-support-hstreamdb-and-tablestore) ([Edit](https://github.com/emqx/blog/blob/main/en/202208/data-integration-support-hstreamdb-and-tablestore.md))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
[PHP](https://www.php.net) is a widely-used open source multi-purpose scripting language, which can be embedded in HTML and is especially suitable for Web development.
[PHP](https://www.php.net/) is a widely-used open-source scripting language that is especially suitable for web development and can be embedded in HTML. It enables developers to build dynamic web applications with ease.

This article mainly introduces how to use the `php-mqtt/client` client library in PHP projects to implement the functions of connection, subscription, unsubscribing, message receiving and sending between [MQTT client](https://www.emqx.com/en/blog/mqtt-client-tools) and [MQTT broker](https://github.com/emqx/emqx).
In this guide, we’ll walk you through using the `php-mqtt/client` library to integrate **[MQTT](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt)** into your PHP applications. You’ll learn how to implement essential MQTT functions such as connecting, subscribing, unsubscribing, receiving, and sending messages between the [MQTT client](https://www.emqx.com/en/blog/mqtt-client-tools) and an [MQTT broker](https://www.emqx.com/en/blog/the-ultimate-guide-to-mqtt-broker-comparison) for real-time messaging and IoT applications.

## **Choosing the Best MQTT Client Library for PHP**


## MQTT client library selection

This article chooses the client library `php-mqtt/client`, which has the highest downloads on composer. For more PHP-MQTT client libraries, please view in [Packagist-Search MQTT](https://packagist.org/search/?query =mqtt).

For more documentation about php-mqtt/client, please refer to [Packagist php-mqtt/client](https://packagist.org/packages/php-mqtt/client).
For this tutorial, we'll use the **php-mqtt/client** library, which has the highest number of downloads on **Composer**. It’s a reliable, easy-to-use solution for integrating **MQTT** into PHP applications. If you’re looking for other MQTT client libraries for PHP, you can explore more options on [Packagist](https://packagist.org/search/?query=mqtt).

MQTT communication belongs to a network communication scenario outside the HTTP system. Due to the limitation of PHP characteristics, using the extensions for network communication such as Swoole/Workerman in the PHP system can bring a better experience. Its use will not be repeated in this article. The relevant MQTT client libraries are as follows:

- [workerman/mqtt](https://packagist.org/packages/workerman/mqtt):Asynchronous MQTT client for PHP based on workerman.
- [simps/mqtt](https://packagist.org/packages/simps/mqtt):[MQTT protocol](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt) Analysis and Coroutine Client for PHP.

## **Setting Up Your PHP Project for MQTT Integration**

### Confirm the PHP Version

## Project initialization

### Confirm the PHP version

This project uses 7.4.21 for development and testing. Readers can confirm the PHP version with the following command.
To get started, ensure that you are using PHP version 7.4.21 or higher. You can check your PHP version by running the following command in your terminal:

```
```php
php --version

PHP 7.4.21 (cli) (built: Jul 12 2021 11:52:30) ( NTS )
Expand All @@ -32,49 +26,29 @@ Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies
```

### Use Composer to install `php-mqtt/client`
### **Install the** `php-mqtt/client` **Library with Composer**

Composer is a dependency management tool for PHP, which can manage all the dependencies your PHP project needs.

```bash
composer require php-mqtt/client
```
Composer is a powerful dependency management tool for PHP that simplifies the installation of required libraries for your PHP projects.

To install the **php-mqtt/client** library, run the following command:

```shell
composer require php-mqtt/client
```

## PHP MQTT usage
## PHP MQTT Usage

### Connect to the MQTT broker
### Connect to the MQTT Broker

This article will use the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker) provided by EMQX, which is created on EMQX's [MQTT Cloud Service](https://www.emqx.com/en/cloud). The server access information is as follows:
This article will use the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker) provided by EMQX, which is created on EMQX [MQTT Platform](https://www.emqx.com/en/cloud). The server access information is as follows:

- Broker: **broker.emqx.io**
- TCP Port: **1883**
- SSL/TLS Port: **8883**

<section
class="promotion-pdf"
style="border-radius: 16px; background: linear-gradient(102deg, #edf6ff 1.81%, #eff2ff 97.99%); padding: 32px 48px;"
>
<div style="flex-shrink: 0;">
<img loading="lazy" src="https://assets.emqx.com/images/b4cff1e553053873a87c4fa8713b99bc.png" alt="Open Manufacturing Hub" width="160" height="226">
</div>
<div>
<div class="promotion-pdf__title" style="
line-height: 1.2;
">
A Practical Guide to MQTT Broker Selection
</div>
<div class="promotion-pdf__desc">
Download this practical guide and learn what to consider when choosing an MQTT broker.
</div>
<a href="https://www.emqx.com/en/resources/a-practical-guide-to-mqtt-broker-selection?utm_campaign=embedded-a-practical-guide-to-mqtt-broker-selection&from=blog-how-to-use-mqtt-in-php" class="button is-gradient">Get the eBook →</a>
</div>
</section>

#### Import composer autoload file and `php-mqtt/client`

```php
```shell
require('vendor/autoload.php');

use \PhpMqtt\Client\MqttClient;
Expand Down Expand Up @@ -147,9 +121,9 @@ for ($i = 0; $i< 10; $i++) {
$mqtt->loop(true);
```

### Complete code
### Complete Code

Server connection, message publishing and receiving code.
Here’s the full PHP code to connect, publish, and subscribe to the MQTT broker:

```php
<?php
Expand Down Expand Up @@ -206,33 +180,29 @@ for ($i = 0; $i< 10; $i++) {
}

$mqtt->loop(true);

```


## Test

After running the MQTT message publishing code, we will see that the client has successfully connected, and the messages have been published one by one and received successfully:

```bash
php pubsub_tcp.php
```shell
php pubsub_tcp.php
```

![PHP MQTT Test](https://assets.emqx.com/images/61618d56823886f101feaf6741a20c3f.png)

## **Summary and Next Steps**

## Summary

So far, we have used the **php-mqtt/client** to connect to the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker), and implemented the connection, message publishing and subscription between the test client and the MQTT server.
In this tutorial, we demonstrated how to use the **php-mqtt/client** library in PHP to connect to an MQTT broker, publish and subscribe to [MQTT topics](https://www.emqx.com/en/blog/advanced-features-of-mqtt-topics), and receive real-time messages. This is a foundational step for building powerful, real-time applications using PHP and MQTT.

Next, you can check out [The Easy-to-understand Guide to MQTT Protocol](https://www.emqx.com/en/mqtt-guide) series of articles provided by EMQ to learn about MQTT protocol features, explore more advanced applications of MQTT, and get started with MQTT application and service development.
For more advanced MQTT tutorials, check out our [The Easy-to-understand Guide to MQTT Protocol](https://www.emqx.com/en/mqtt-guide) series to explore more advanced applications of MQTT.



<section class="promotion">
<div>
Try EMQX Cloud for Free
<div class="is-size-14 is-text-normal has-text-weight-normal">No credit card required</div>
Talk to an Expert
</div>
<a href="https://accounts.emqx.com/signup?continue=https://cloud-intl.emqx.com/console/deployments/0?oper=new" class="button is-gradient px-5">Get Started →</a>
<a href="https://www.emqx.com/en/contact?product=solutions" class="button is-gradient">Contact Us →</a>
</section>
Loading