skip to Main Content

I tried to connect MQTTS with my HTTPS server but it didn’t work. It works fine on MQTTX but with PHP it doesn’t connect.

<?php 

$server   = 'myServer';
$port     = '8883';
$clientId = 'testing';
$username = 'XXXX';
$password = 'XXXX';
$clean_session = false;

$connectionSettings  = new ConnectionSettings();
$connectionSettings->setUsername($username)
    ->setPassword($password)
    ->setKeepAliveInterval(60)
    ->setLastWillTopic('mytopic')
    ->setLastWillMessage('client disconnect')
    ->setLastWillQualityOfService(1);

$mqtt = new MqttClient($server, $port, $clientId);
$mqtt->connect($connectionSettings, $clean_session);

$mqtt->subscribe('mytopic/respond', function ($topic, $message) use ($mqtt) {
    echo $message;
}, 2);

$mqtt->close();
$mqtt->interrupt();
?>

How to connect MQTTS with PHP.

3

Answers


  1. Chosen as BEST ANSWER

    For connecting MQTTS we need to add three additional params into the connectionSettings

    ->setUseTls(true)
    ->setTlsSelfSignedAllowed(true) // Allow self-signed certificates. Discouraged for production use.
    ->setTlsVerifyPeer(false)           // Do not require the self-signed certificate to match the host. Discouraged.
    

    https://github.com/php-mqtt/client-examples/blob/master/03_connection_settings/02_use_tls_without_client_certificate.php

    <?php 
    
    $server   = 'myServer';
    $port     = '8883';
    $clientId = 'testing';
    $username = 'XXXX';
    $password = 'XXXX';
    $clean_session = false;
    
    $connectionSettings = (new ConnectionSettings)
        ->setUsername($username)
        ->setPassword($password)
        ->setKeepAliveInterval(60)
        ->setLastWillTopic('mytopic')
        ->setLastWillMessage('client disconnect')
        ->setUseTls(true)
        ->setTlsSelfSignedAllowed(true) // Allow self-signed certificates. Discouraged for production use.
        ->setTlsVerifyPeer(false)           // Do not require the self-signed certificate to match the host. Discouraged.
        ->setLastWillQualityOfService(1);
    
    $mqtt = new MqttClient($server, $port, $clientId);
    $mqtt->connect($connectionSettings, $clean_session);
    
    $mqtt->subscribe('mytopic/respond', function ($topic, $message) use ($mqtt) {
        echo $message;
    }, 2);
    
    $mqtt->close();
    $mqtt->interrupt();
    ?>
    

  2. Looking at the source code for the phpMQTT library you need to pass a cafile in the constructor to enable a SSL/TLS connection.

    $mqtt = new MqttClient($server, $port, $clientId, $cafile);
    

    Where the cafile is the path to a CA certificate to validate the broker.

    Login or Signup to reply.
  3. OK, first answer was against the wrong phpMQTT library, trying again.

    The correct library is here: https://github.com/php-mqtt/client

    From the doc:

    // This flag determines if TLS should be used for the connection. The port which is used to

    // connect to the broker must support TLS connections.

    ->setUseTls(false)
    

    e.g.

    $connectionSettings->setUsername($username)
        ->setPassword($password)
        ->setKeepAliveInterval(60)
        ->setLastWillTopic('mytopic')
        ->setLastWillMessage('client disconnect')
        ->setUseTls(true)
        ->setLastWillQualityOfService(1);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search