Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/binsoul/net-mqtt-client-react
Asynchronous MQTT client built on React
https://github.com/binsoul/net-mqtt-client-react
mqtt mqtt-client reactphp
Last synced: 10 days ago
JSON representation
Asynchronous MQTT client built on React
- Host: GitHub
- URL: https://github.com/binsoul/net-mqtt-client-react
- Owner: binsoul
- License: mit
- Created: 2015-11-22T18:18:59.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-29T07:01:18.000Z (about 3 years ago)
- Last Synced: 2024-10-14T20:10:25.549Z (22 days ago)
- Topics: mqtt, mqtt-client, reactphp
- Language: PHP
- Homepage:
- Size: 55.7 KB
- Stars: 47
- Watchers: 7
- Forks: 24
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# net-mqtt-client-react
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Total Downloads][ico-downloads]][link-downloads]
[![Build Status](https://travis-ci.org/binsoul/net-mqtt-client-react.svg?branch=master)](https://travis-ci.org/binsoul/net-mqtt-client-react)This package provides an asynchronous MQTT client built on the [React socket](https://github.com/reactphp/socket) library. All client methods return a promise which is fulfilled if the operation succeeded or rejected if the operation failed. Incoming messages of subscribed topics are delivered via the "message" event.
## Install
Via composer:
``` bash
$ composer require binsoul/net-mqtt-client-react
```## Example
Connect to a public broker and run forever.
``` php
createCached('8.8.8.8', $loop));
$client = new ReactMqttClient($connector, $loop);// Bind to events
$client->on('open', function () use ($client) {
// Network connection established
echo sprintf("Open: %s:%d\n", $client->getHost(), $client->getPort());
});$client->on('close', function () use ($client, $loop) {
// Network connection closed
echo sprintf("Close: %s:%d\n", $client->getHost(), $client->getPort());$loop->stop();
});$client->on('connect', function (Connection $connection) {
// Broker connected
echo sprintf("Connect: client=%s\n", $connection->getClientID());
});$client->on('disconnect', function (Connection $connection) {
// Broker disconnected
echo sprintf("Disconnect: client=%s\n", $connection->getClientID());
});$client->on('message', function (Message $message) {
// Incoming message
echo 'Message';if ($message->isDuplicate()) {
echo ' (duplicate)';
}if ($message->isRetained()) {
echo ' (retained)';
}echo ': '.$message->getTopic().' => '.mb_strimwidth($message->getPayload(), 0, 50, '...');
echo "\n";
});$client->on('warning', function (\Exception $e) {
echo sprintf("Warning: %s\n", $e->getMessage());
});$client->on('error', function (\Exception $e) use ($loop) {
echo sprintf("Error: %s\n", $e->getMessage());$loop->stop();
});// Connect to broker
$client->connect('test.mosquitto.org')->then(
function () use ($client) {
// Subscribe to all topics
$client->subscribe(new DefaultSubscription('#'))
->then(function (Subscription $subscription) {
echo sprintf("Subscribe: %s\n", $subscription->getFilter());
})
->otherwise(function (\Exception $e) {
echo sprintf("Error: %s\n", $e->getMessage());
});// Publish humidity once
$client->publish(new DefaultMessage('sensors/humidity', '55%'))
->then(function (Message $message) {
echo sprintf("Publish: %s => %s\n", $message->getTopic(), $message->getPayload());
})
->otherwise(function (\Exception $e) {
echo sprintf("Error: %s\n", $e->getMessage());
});// Publish a random temperature every 10 seconds
$generator = function () {
return mt_rand(-20, 30);
};$client->publishPeriodically(10, new DefaultMessage('sensors/temperature'), $generator)
->progress(function (Message $message) {
echo sprintf("Publish: %s => %s\n", $message->getTopic(), $message->getPayload());
})
->otherwise(function (\Exception $e) {
echo sprintf("Error: %s\n", $e->getMessage());
});
}
);$loop->run();
```## Testing
``` bash
$ composer test
```## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
[ico-version]: https://img.shields.io/packagist/v/binsoul/net-mqtt-client-react.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/binsoul/net-mqtt-client-react.svg?style=flat-square[link-packagist]: https://packagist.org/packages/binsoul/net-mqtt-client-react
[link-downloads]: https://packagist.org/packages/binsoul/net-mqtt-client-react
[link-author]: https://github.com/binsoul