nailfor/php-queue-client

a queue broker client library for php

v1.2.8 2021-09-28 10:53 UTC

This package is not auto-updated.

Last update: 2024-04-09 22:35:53 UTC


README

phpQueueClient is an client library for AMQP and MQTT queues server. Its based on the reactPHP socket-client

Goal

Goal of this project is easy to use both AMQP and MQTT client for PHP in a modern architecture without using any php modules. Original idea taken from https://github.com/oliverlorenz/phpMqttClient

Example publish

use nailfor\queue\ClientFactory;
use nailfor\queue\protocol\MQTT;

class MQTTClass 
{
    public function subscribe()
    {
        $url    = '127.0.0.1:5672';

        $options = [
            //'username'  => '',
            //'password'  => '',
            'clientId'  => 'php',
            'cleanSession' => false,

            'topics'    => [
                'topic_name' => [
                    //this flag clear message after reciev. Default true
                    //'clear'     => false,
                    'qos'       => 1, //only for MQTT
                    'message'    => 'hello from topic',
                ],
                'capital_name' => [
                    'qos'       => 2, //only for MQTT
                    'message'    => 'hello from capital',
                ],
            ],        
        ];

        $protocol = new MQTT; //default AMQP
        ClientFactory::publish($url, $options, [$this, 'onError'], $protocol);
    }
}

Example subscribe

use nailfor\queue\ClientFactory;
use nailfor\queue\protocol\MQTT;
use Illuminate\Support\Facades\Log;

class MQTTClass 
{
    public function onMessage($packet)
    {
        //...
    }

    public function onCapitalMessage($packet)
    {
        //...
    }

    public function onError($reason) {
        echo $reason->getMessage(). PHP_EOL;
        exit;
    }

    public function subscribe()
    {
        $url    = '127.0.0.1:5672';

        $options = [
            //'username'  => '',
            //'password'  => '',
            'clientId'  => 'php',
            'cleanSession' => false,

            'topics'    => [
                'topic_name' => [
                    //this flag clear message after reciev. Default true
                    //'clear'     => false,
                    'qos'       => 1, //only for MQTT
                    'events'    => [
                        'PUBLISH' => [$this, 'onMessage'],
                    ],
                ],
                'capital_name' => [
                    'qos'       => 0, //only for MQTT
                    'events'    => [
                        'PUBLISH' => [$this, 'onCapitalMessage'],
                    ],
                ],
            ],        
        ];

        $protocol = new MQTT; //default AMQP
        $logger = Log::channel('stderr'); //default null
        ClientFactory::run($url, $options, [$this, 'onError'], $protocol, $logger);
    }
}

also commands are available

ClientFactory::run //subscribe and get messages

ClientFactory::publish

ClientFactory::unsubscribe

ClientFactory::ping

The Logger suppurt 3 level of details: notice, info and debug. Offcourse you can disable it set null in last param

Notice - (Oct 21th, 2020)

Currently work:

  • AMQP implementation:

  • publish.. not yet

  • subscribe

  • unsubscribe

  • MQTT implementation:

    • qos 1
    • qos 2
  • subscribe

  • unsubscribe

  • publish