Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dipta_dey's avatar

[1000] Establishing a connection to the MQTT broker failed: Socket error

When i try to connect my mqtt i face error ---- MQTT connection failed: [4] Establishing a connection to the MQTT broker failed: The broker did not acknowledge the connection attempt within the configured connection timeout period. Note : Package name - - - - php-mqtt/laravel-client

and my web.php is

Route::get('/mqtt', function () {
    try {
        set_time_limit(180);
        $client = MQTT::connection();
        $client->connect();
        $client->disconnect();
        return "MQTT connection successful.";
    } catch (\Exception $e) {
        return "MQTT connection failed: " . $e->getMessage();
    }
});

and .env is

MQTT_HOST=b54df56d2b12423fb4fd74426d3b1123.s1.eu.hivemq.cloud
MQTT_AUTH_USERNAME=1688062828534.test
MQTT_AUTH_PASSWORD=,CyGDZ4dbw!1
MQTT_PORT=8883

So how can i fix this error or connect ..

1 like
2 replies
nexxai's avatar

That sounds like a connection time out; is there a firewall on the MQTT host that you need to add your Laravel server's IP to?

1 like
srajiv9496's avatar
Level 3

##Even I'm facing the same issue as you:

MQTT Client Error: [1000] Establishing a connection to the MQTT broker failed: Socket error [0]: php_network_getaddresses: getaddrinfo for mqtt.geniusvision.in failed: Try again

Here is my code: ` public function handle() { $broker = 'mqtt.geniusvision.in'; $port = 1883;

    $client = new MqttClient($broker, $port);

    try {
        $client->connect();
        $this->info('Connected to the MQTT Broker.');

        $client->subscribe('v1/sensor-tag/800350/telemetry', function ($topic, $message) {
            $this->info("Received data on topic [$topic]: $message");

            $data = json_decode($message, true);
            if (isset($data['temperature'], $data['humidity'])) {
                SensorData::create([
                    'sensor_id' => '800350',
                    'temperature' => $data['temperature'],
                    'humidity' => $data['humidity'],
                ]);
                $this->info("Data saved to the database.");
            } else {
                $this->error("Invalid data format.");
            }
        });

        $client->loop(true);

    } catch (MqttClientException $e) {
        $this->error("MQTT Client Error: " . $e->getMessage());
    }
}

`

1 like

Please or to participate in this conversation.