0

I'm new to the PHP world, facing problem while connecting to MQTT.

I'm using the phpMQTT.php library, and I'm using the IP address to connect to the MQTT broker. I'm trying to publish to MQTT broker, getting error in phpMQTT.php library file

The error is: stream_socket_client(): unable to connect to tcp://...*:8083 (Connection timed out)

facing problem in below code:

if ($this->cafile) {
            $socketContext = stream_context_create(["ssl" => [
                "verify_peer_name" => true,
                "cafile" => $this->cafile
                ]]);
            $this->socket = stream_socket_client("tls://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $socketContext);
        } else {
            $this->socket = stream_socket_client("tcp://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT);
        }
Pavan A
  • 21
  • 1
  • 2
  • Please do not just create a new user to re-ask a closed [question](https://stackoverflow.com/questions/61174250/how-to-connect-to-mqtt-using-php-codepublish-and-subscribe). Just adding the error without the code you used doesn't really help us get to the root cause. Please read the [doc](https://stackoverflow.com/help/how-to-ask) about what a good question need – hardillb Apr 12 '20 at 19:06

2 Answers2

0

From the phpMQTT.php library
( source at https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ) ,
you have to set the following details as shown in the source code.

/* sets the broker details */  

function broker($address, $port, $clientid, $cafile = NULL){
    $this->address = $address;
    $this->port = $port;
    $this->clientid = $clientid;
    $this->cafile = $cafile;
}

If you have a firewall running - do open the port that you are using as well.

MarcoZen
  • 1,556
  • 22
  • 27
0

If everything settings fine, please consider your hosting server does not allow connection outbound to other ports as 8083. Some hosting servers allows only connection to servers with common HTTP ports as 80, 433.

trung
  • 81
  • 1
  • 1
  • 5