Skip to main content

Connection Setup

MqttClient

An instance of MqttClient needs to be created in order to establish a Courier connection.

val mqttClient = MqttClientFactory.create(
context = context,
mqttConfiguration = mqttConfiguration
)

Connect using MqttClient

val alpnProtocol = "mqtt"
val connectOptions = MqttConnectOptions.Builder()
.serverUris(listof(ServerUri(SERVER_URI, SERVER_PORT)))
.clientId(clientId)
.userName(username)
.password(password)
.keepAlive(KeepAlive(timeSeconds = keepAliveSeconds))
.cleanSession(cleanSessionFlag)
.alpnProtocols(listOf(Protocol(alpnProtocol)))
.build()

mqttClient.connect(connectOptions)

Disconnect using MqttClient

mqttClient.disconnect()

MqttConnectOptions

MqttConnectOptions represents the properties of the underlying MQTT connection in Courier.

  • Server URIs : List of ServerUri representing the host and port of an MQTT broker.

  • Client Id : Unique ID of the MQTT client.

  • Username : Username of the MQTT client.

  • Password : Password of the MQTT client.

  • KeepAlive Interval : Interval at which keep alive packets are sent for the MQTT connection.

  • Clean Session Flag : When clean session is false, a persistent connection is created. Otherwise, non-persistent connection is created and all persisted information is cleared from both client and broker.

  • Read Timeout : Read timeout of the SSL/TCP socket created for the MQTT connection.

  • MQTT protocol version : It can be either VERSION_3_1 or VERSION_3_1_1.

  • User properties : Custom user properties appended to the CONNECT packet.

  • Socket Factory : Sets the socket factory used to create connections. If unset, the SocketFactory.getDefault socket factory will be used. Set shouldUseNewSSLFlow to true to enable this.

  • SSL Socket Factory: Sets the socket factory and trust manager used to secure MQTT connections. If unset, the system defaults will be used. Set shouldUseNewSSLFlow to true to enable this.

  • Connection Spec: Specifies configuration for the socket connection that MQTT traffic travels through. This includes the TLS version and cipher suites to use when negotiating a secure connection. Set shouldUseNewSSLFlow to true to enable this.

  • ALPN Protocols: Configure the alpn protocols used by this client to communicate with MQTT broker. Set shouldUseNewSSLFlow to true to enable this.