Closed APTEMOH closed 3 years ago
Have you read the README and looked at the linked examples? The examples are thoroughly documented and self-eplanatory.
Should you still have issues after looking at the documentation and examples, please provide more information (error description, details about your setup, code, ...) when reopening this issue, or I'll not be able to help.
require_once __DIR__ . '/vendor/autoload.php';
// https://github.com/php-mqtt/client
$server = 'srv2.clusterfly.ru';
$port = 9991;
$clientId = 'user_***_arduino';
$connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
->setKeepAliveInterval(60)
->setUsername("user")
->setPassword("pass");
$mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
$c = $mqtt->connect($connectionSettings, true);
printf("client connected\n");
$mqtt->subscribe('user_***/room1/lamp', function ($topic, $message) {
printf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0, true);
$mqtt->disconnect();
You are missing $mqtt->loop(true)
before $mqtt->disconnect()
. Currently, you are subscribing and immediately disconnecting right after. Or in other words:
Client: Hey broker, I'd like to know about changes on
user_***/room1/lamp
. Broker: Sure thing, I'll notify you! Client: Hey broker, I'm disconnecting. Broker: Okay, sorry to see you go.
A subscription will always require the application to keep running, which is what $mqtt->loop()
does.
Thanks!
So loop in the browser will not work !? Is it a perpetual cycle?
Do you need such a script in a daemon? For example a supervisor.
Yes and no. Calling subscribe from the browser works but it only makes sense for reliable RPC (Remote Procedure Calls), not for actual subscriptions. This is because a request will be terminated after a few seconds if it does not finish by itself (the default of PHP is 30 seconds I think). If you want to receive and process messages for hours, you really need to run it as daemon. If you use supervisor for it or something else, is totally up to you.
About the internals: yes, loop()
is an infinite event loop which retrieves messages and delivers them to registered callbacks. It also runs hooks, as shown in the examples.
Hey! Thanks for the answer!
A couple more questions, if I may.
I need to take telegrams from the MQTT to the Telegram bot. Two options: base mysql or file.
Question: won't the database fall from frequent updates? Well, what about the file, write data to it every second?
Thanks!
You need to provide a bit more background information about the exact frequency and type/size of data you are planning to receive, store and forward.
Generally speaking, 100 inserts to a MySQL table per second is absolutely no problem. The performance is obviously depending on the server hardware though.
Turn on the light and bring out the temperature.
MQTT -> PHP -> Telegram BOT
Not sure why you need persistence for this. I would use Redis to store a job queue and use a second daemon which processes the jobs (and forwards them to Telegram). Using a framework like Laravel would be of great help for this.
Hey!
Subscription does not work. What to do? How to be?
Thanks!