mqtt = new phpMQTT($host, $port, $client_id); if ($username && $password) { if (!$this->mqtt->connect(true, null, $username, $password)) { exit(1); } } else { if (!$this->mqtt->connect(true, null)) { exit(1); } } } public function subscribe($topic, $callback) { $this->mqtt->subscribe([$topic => ["qos" => 0, "function" => $callback]]); while ($this->mqtt->proc()) {} $this->mqtt->close(); } }