All Projects → binsoul → net-mqtt-client-react

binsoul / net-mqtt-client-react

Licence: MIT license
Asynchronous MQTT client built on React

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to net-mqtt-client-react

Reactphp
Event-driven, non-blocking I/O with PHP.
Stars: ✭ 8,211 (+18146.67%)
Mutual labels:  reactphp
Php Console Spinner
Colorful highly configurable spinner for php cli applications (suitable for async apps)
Stars: ✭ 225 (+400%)
Mutual labels:  reactphp
flutter im demo
📞 Flutter 使用 MQTT实现IM功能
Stars: ✭ 81 (+80%)
Mutual labels:  mqtt-client
Filesystem
Evented filesystem access.
Stars: ✭ 102 (+126.67%)
Mutual labels:  reactphp
Promise
Promises/A implementation for PHP.
Stars: ✭ 2,078 (+4517.78%)
Mutual labels:  reactphp
Http Client
[Deprecated] Event-driven, streaming HTTP client for ReactPHP.
Stars: ✭ 228 (+406.67%)
Mutual labels:  reactphp
Supervisord
Async first supervisord HTTP API Client for PHP 7
Stars: ✭ 14 (-68.89%)
Mutual labels:  reactphp
javascript
Nodejs MQTT client for emitter.io.
Stars: ✭ 27 (-40%)
Mutual labels:  mqtt-client
Hhvmcraft
📦 Minecraft Beta 1.7.3 Server implemented in PHP/HHVM, powered by ReactPHP.
Stars: ✭ 180 (+300%)
Mutual labels:  reactphp
eMQTT5
An embedded MQTTv5 client in C++ with minimal footprint, maximal performance
Stars: ✭ 51 (+13.33%)
Mutual labels:  mqtt-client
Zanzara
Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
Stars: ✭ 107 (+137.78%)
Mutual labels:  reactphp
Demo
Demo for DriftPHP
Stars: ✭ 117 (+160%)
Mutual labels:  reactphp
StriderMqtt
A very thin MQTT client
Stars: ✭ 21 (-53.33%)
Mutual labels:  mqtt-client
React Restify
A ReactPHP framework to create RESTfull api
Stars: ✭ 87 (+93.33%)
Mutual labels:  reactphp
python-mqtt-client-shell
Python-based MQTT client command shell
Stars: ✭ 45 (+0%)
Mutual labels:  mqtt-client
Event Loop
ReactPHP's core reactor event loop that libraries can use for evented I/O.
Stars: ✭ 945 (+2000%)
Mutual labels:  reactphp
Child Process
Event-driven library for executing child processes with ReactPHP.
Stars: ✭ 225 (+400%)
Mutual labels:  reactphp
MQTT-Board
Diagnostic-oriented MQTT client tool. Supports MQTT 5.0 and 3.1.X protocols, connections to multiple brokers, MQTT operations logs and multiple subscribe widgets with unique/history topic filtering mode. Saves configuration in browser's local cache.
Stars: ✭ 81 (+80%)
Mutual labels:  mqtt-client
homie-device
NodeJS port of Homie for IoT
Stars: ✭ 20 (-55.56%)
Mutual labels:  mqtt-client
Android-MQTT-Demo
An android application to demonstrate the complete MQTT lifecycle.
Stars: ✭ 31 (-31.11%)
Mutual labels:  mqtt-client

net-mqtt-client-react

Latest Version on Packagist Software License Total Downloads Build Status

This package provides an asynchronous MQTT client built on the React 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:

$ composer require binsoul/net-mqtt-client-react

Example

Connect to a public broker and run forever.

<?php

use BinSoul\Net\Mqtt\Client\React\ReactMqttClient;
use BinSoul\Net\Mqtt\Connection;
use BinSoul\Net\Mqtt\DefaultMessage;
use BinSoul\Net\Mqtt\DefaultSubscription;
use BinSoul\Net\Mqtt\Message;
use BinSoul\Net\Mqtt\Subscription;
use React\Socket\DnsConnector;
use React\Socket\TcpConnector;

include 'vendor/autoload.php';

// Setup client
$loop = \React\EventLoop\Factory::create();
$dnsResolverFactory = new \React\Dns\Resolver\Factory();
$connector = new DnsConnector(new TcpConnector($loop), $dnsResolverFactory->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

$ composer test

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].