Link Search Menu Expand Document Edit GitHub

Client Configuration

A MQTT client can be created and configured by using a fluent builder pattern.

MqttClient.builder()
        .identifier(UUID.randomUUID().toString())
        .serverHost("broker.hivemq.com")
        .serverPort(1883)
        ...;

General configuration

Method nameDescriptionDefault value
identifierThe unique identifier of the MQTT clientThe server creates an identifier for the client
executorConfigConfiguration of used Threads, see Thread ManagementDefault Netty event loop and Schedulers.comutation() for callbacks

Transport configuration

Method nameDescriptionDefault value
serverAddressThe address (host + port) of the MQTT serverSee serverHost and serverPort
serverHostThe host name or IP address of the MQTT serverlocalhost
serverPortThe port of the MQTT server- 1883
- 8883 (SSL/TLS)
- 80 (WebSocket)
- 443 (Secure WebSocket)
sslConfig
sslWithDefaultConfig
Secure transport configuration (SSL/TLS), see Securitynone
webSocketConfig
webSocketWithDefaultConfig
WebSocket transport configuration, see Transportsnone
transportConfigTransport configuration which combines:
- server address (host + port)
- secure transport configuration
- WebSocket transport configuration
See
- serverAddress /Host /Port
- sslConfig
- webSocketConfig

Lifecycle configuration

Method nameDescriptionDefault value
automaticReconnect
automaticReconnectWithDefaultConfig
Automatic reconnect configurationnone
addConnectedListenerAdds a listener that is notified when the client is connectednone
addDisconnectedListenerAdds a listener that is notified when the client is disconnectednone

You can not build an instance of MqttClient directly, but a version specific Mqtt5Client or Mqtt3Client.

  • You can use the generic MqttClientBuilder to set the above properties and then switch to the builder for the Mqtt5Client by calling useMqttVersion5():

    MqttClientBuilder clientBuilder = MqttClient.builder()
            .identifier(UUID.randomUUID().toString())
            .serverHost("broker.hivemq.com")
    
    Mqtt5Client client = clientBuilder.useMqttVersion5().build();
    

    Alternatively you can directly use a Mqtt5ClientBuilder:

    Mqtt5Client client = Mqtt5Client.builder()
            .identifier(UUID.randomUUID().toString())
            .serverHost("broker.hivemq.com")
            .build();
    

    The Mqtt5ClientBuilder has the following additional configuration methods:

    Method nameDescriptionDefault value
    advancedConfigAdvanced configuration options for special requirementsSensible default values, you do not have to care about these if you do not require any of the advanced options
  • You can use the generic MqttClientBuilder to set the above properties and then switch to the builder for the Mqtt3Client by calling useMqttVersion3():

    MqttClientBuilder clientBuilder = MqttClient.builder()
            .identifier(UUID.randomUUID().toString())
            .serverHost("broker.hivemq.com")
    
    Mqtt3Client client = clientBuilder.useMqttVersion3().build();
    

    Alternatively you can directly use a Mqtt3ClientBuilder:

    Mqtt3Client client = Mqtt3Client.builder()
            .identifier(UUID.randomUUID().toString())
            .serverHost("broker.hivemq.com")
            .build();