1. Go to this page and download the library: Download salmanzafar/laravel-mqtt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Salman\Mqtt\MqttClass\Mqtt;
public function SendMsgViaMqtt($topic, $message)
{
$mqtt = new Mqtt();
$client_id = Auth::user()->id;
$output = $mqtt->ConnectAndPublish($topic, $message, $client_id);
if ($output === true)
{
return "published";
}
return "Failed";
}
use Mqtt;
public function SendMsgViaMqtt($topic, $message)
{
$client_id = Auth::user()->id;
$output = Mqtt::ConnectAndPublish($topic, $message, $client_id);
if ($output === true)
{
return "published";
}
return "Failed";
}
use Salman\Mqtt\MqttClass\Mqtt;
public function SubscribetoTopic($topic)
{
$mqtt = new Mqtt();
$client_id = Auth::user()->id;
$mqtt->ConnectAndSubscribe($topic, function($topic, $msg){
echo "Msg Received: \n";
echo "Topic: {$topic}\n\n";
echo "\t$msg\n\n";
}, $client_id);
}
use Mqtt;
public function SubscribetoTopic($topic)
{
//You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']
Mqtt::ConnectAndSubscribe($topic, function($topic, $msg){
echo "Msg Received: \n";
echo "Topic: {$topic}\n\n";
echo "\t$msg\n\n";
},$client_id);
}
public function SendMsgViaMqtt($topic, $message)
{
$client_id = Auth::user()->id;
$output = connectToPublish($topic, $message, $client_id);
if ($output === true)
{
return "published";
}
return "Failed";
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.