Using Mosquitto-PHP in laravel

topic description

there is always an error that MosquittoClient cannot find when using Mosquitto-PHP, in laravel

sources of topics and their own ideas

I have compiled mosquitto.so, and added php.ini, and have no problem using the following code:

<?php

$c = new Mosquitto\Client;
$c->onConnect(function() use ($c) {
    $c->publish("mgdm/test", "Hello", 2);
    $c->disconnect();
});

$c->connect("test.mosquitto.org");

// Loop around to permit the library to do its work
// This function will call the callback defined in `onConnect()`
// and disconnect cleanly when the message has been sent
$c->loopForever();

echo "Finished\n";

because the laravel framework is used in the project, after I put the above code in it, there is always an error of SymfonyComponentDebugExceptionFatalThrowableError: Class" MosquittoClient" not found.

The code for

laravel is as follows:

<?php                                                                                                                                                                  
namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Log;
use Mosquitto\Client;

class QueueService
{
    public function __construct(){
    }   

    public function send($payload, $cb){
        if(!is_callable($cb)){
            $cb = function(){};
        }   

        if(!$payload){
            $cb([
                "status" => true,
                "code" => "", 
                "data" => ["c" => $c, "send" => $send],
                "message" => ""
            ]); 
            return;
        }   

        $broker = env("AMQP_ACTIVEMQ_BROKER", "");
        $port = env("AMQP_ACTIVEMQ_PORT", "");
        $topic = env("AMQP_ACTIVEMQ_TOPIC", "");
        if(!$broker || !$topic || !$port){
            $cb([
                "status" => true,
                "code" => "", 
                "data" => [], 
                "message" => ": Broker  Topic "
            ]);
            return;
        }

        try{
            $url = $broker.":".$port;
            //$c = new \Mosquitto\Client;
            $c = new Client;
            $c->onConnect(function($rc, $message) use ($c, $topic, $payload) {
                $c->publish($topic, $payload, 2);
            });

            $c->onPublish(function($mid) use($c){
                $cb([
                    "status" => true,
                    "code" => "",
                    "data" => [
                        "mid" => $mid
                    ],
                    "message" => ""
                ]);
                $c->disconnect();
            });

            $c->connect($url);
            $c->loopForever();
        } catch (\Exception $e) {
            $cb([
                "status" => false,
                "code" => "",
                "data" => [],  
                                "message" => ": ".$e->getMessage()
            ]);
            return;
        }
    }
}

does anyone know how to use the MosquittoClient class in laravel?

Apr.05,2021

namespace problem. You can specify the path when creating a new object, as follows:

$c = new \Mosquitto\Client;
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb3ee4-4826d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb3ee4-4826d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?