MQTT version of micro message queue is designed for mobile Internet (MI), Internet of Things (IoT) field of messaging products, covering interactive live, financial payment, intelligent catering, instant chat, mobile Apps, smart devices, Internet of vehicles and other application scenarios; Through the comprehensive support of MQTT, WebSocket and other protocols, the two-way communication between the connection end and the cloud can realize message communication between C2C, C2B, B2C and other business scenarios, supporting tens of millions of devices and message concurrency.
The MOSQUITo-PHP SDK recommended by PHP in the AliCloud documentation is mosquito-PHP, a MOSQUITo-PHP MQTT extension based on callbacks and asynchronous operations that also relies on libmosquitto.
Simps/MQTT is a pure PHP code implementation of protocol parsing, the client implementation of Swoole based synchronous blocking client and coroutine client, can be used in PHP-FPM and CLI two modes.
In addition, SIMPS/MQTT supports MQTT 5.0 protocol, which is the first class library of PHP that supports MQTT 5.0 protocol. If MQTT version of micro-message queue of Ali Cloud supports MQTT 5.0 protocol in the later stage, it can be upgraded seamlessly.
The following sample code uses simps/ MQTT to implement mosquito-PHP provided earlier
git clone https://github.com/simps-cloud/aliyun-mqtt.git
cd aliyun-mqtt
composer install
Copy the code
The sample code only implements the test logic and needs to be further refined for specific business use.
- The configuration file
config.php
return [
'access_key'= >' '.// Aliyun account AccessKey
'secret_key'= >' '.// Ali cloud account SecretKey
'end_point'= >' '.// Access point address, obtained from console after purchasing instance
'instance_id'= >' '.// Instance ID, obtained from console after purchase
'topic'= >' '.// MQTT Topic, where the first level Topic needs to be applied in advance at the MQTT console
'group_id'= >' '.// the MQTT client ID prefix, GroupID, needs to be applied in the MQTT console
];
Copy the code
- Example using MQTT messaging alone
sendMessageToMQTT.php
include __DIR__ . '/vendor/autoload.php';
use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use function Swoole\Coroutine\run;
run(function () {
$config = require_once __DIR__ . '/config.php';
// MQTT Client ID suffix, DeviceId, which can be specified by the service side freely. Ensure that it is globally unique and prohibit two client connections from using the same ID
$deviceId = Client::genClientID();
$qos = 0;
$port = 1883;
$keepalive = 90;
$cleanSession = true;
$clientId = $config['group_id'].'@ @ @' . $deviceId;
echo "ClientId: {$clientId}", PHP_EOL;
// Set authentication parameters and calculate username and password by referring to the MQTT client authentication code
$username = 'Signature|' . $config['access_key'].'|' . $config['instance_id'];
$sigStr = hash_hmac("sha1".$clientId.$config['secret_key'].true);
$password = base64_encode($sigStr);
echo "UserName: {$username} \r\nPassword: {$password}", PHP_EOL;
// Initialize the client configuration
$clientConfig = new ClientConfig();
$clientConfig->setUserName($username)
->setPassword($password)
->setClientId($clientId)
->setKeepAlive($keepalive)
->setMaxAttempts(0)
->setSwooleConfig([
'open_mqtt_protocol'= >true.'package_max_length'= >2 * 1024 * 1024,]);try {
// Initialize the client
$client = new Client($config['end_point'].$port.$clientConfig);
$connect = $client->connect($cleanSession);
// Connection status
var_dump($connect);
$topics[$config['topic']] = $qos;
$subStatus = $client->subscribe($topics);
// Subscription status
var_dump($subStatus);
$publishStatus = $client->publish($config['topic']."Hello MQTT PHP Demo".$qos);
// Publish status
var_dump($publishStatus);
$buffer = $client->recv();
// Subscribe to receive messages
var_dump($buffer);
echo 'Finished';
} catch (\Throwable $e) {
echo $e->getMessage(); }});Copy the code
- Examples of MQTT signatures
connectUseSignatureMode.php
- MQTT Token sample
connectUseTokenMode.php
- MQTT send order message RocketMQ subscribe order message example
sendOrderMessage.php
- P2P sending and receiving mode
sendP2PMessageToMQTT.php