> For the complete documentation index, see [llms.txt](https://oci-pronghorn.gitbook.io/greenlightning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://oci-pronghorn.gitbook.io/greenlightning/chapter-2-configuration/mqtt-client.md).

# MQTT Configuration

GreenLightning has built-in MQTT support for Internet-of-Things projects or other types of applications. See MQTT Client for more information on how to create a MQTT client with GreenLightning. Learn how to create a MQTT Client [here](/greenlightning/chapter-6-clients/mqtt-client.md).

To enable MQTT support and to create a custom MQTT configuration, use the following code:

{% code title="MQTTClient.java" %}

```java
public class MQTTClient implements GreenApp {
    private MQTTBridge mqttConfig;
    @Override
	public void declareConfiguration(Builder builder) {
	      mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200);
	}
}
```

{% endcode %}

You can add more configurations by appending them to the last configuration:

{% code title="MQTTClient.java" %}

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .cleanSession(true); //example of an extra MQTT configuration
```

{% endcode %}

## Configurations

### useMQTT(String host, int port, String clientName, int maxInFlight)

Enables MQTT usage. `maxInFlight` is an optional argument that lets you specify the maximum number of supported in-flight messages.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200);
```

### cleanSession(boolean b)

Lets broker know if a clean session is required on connection. See [MQTT Essentials Part 7](https://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages) for more information about this flag.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .cleanSession(true);
```

### keepAliveSeconds(int sec)

If no data is sent or received over an open connection for a certain time period then the client will generate a *PINGREQ* and expect to receive a *PINGRESP* from the broker. This specifies that interval in seconds.

This message exchange confirms that the connection is open and working.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .keepAliveSeconds(10);
```

### lastWill(String topic, boolean retain, MQTTQoS qos, Writable payload)

When the client disconnects, this will be the last message sent before disconnect is finished. `topic` is the topic the message should be broadcast to, `retain` determines if a client that tries to subscribe to the last will topic will receive the last will message or not, `qos` specifies [quality of service](https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels) level for MQTT, and `payload` is the payload sent in the last will message.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .lastWill("last/will", false, MQTTQoS.atLeastOnce, w -> {w.writeBoolean(true);});
```

### useTLS(optional: TLSCertificates tlsCertificates)

Enable TLS for MQTT connection. Specify custom certificates if required (otherwise uses default, see [here](/greenlightning/chapter-2-configuration/tls-certificates.md)).

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .useTLS();
```

### authentication(String user, String pass, optional: TLSCertificates certificates)

If your broker requires username and password authentication, set it here. You can optionally pass in TLS certificates if broker is using TLS with authentication.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .authentication("jperalta", "password");
```

### transmissionQoS(MQTTQoS qos)

Sets the default quality-of-service for message transmission.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .transmissionQoS(MQTTQoS.atLeastOnce);
```

### subscriptionQoS(MQTTQoS qos)

Sets the default quality-of-service for subscriptions.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .subscriptionQoS(MQTTQoS.atLeastOnce);
```

### transmissionRetain(boolean b)

Lets broker know if transmissions should be retained or not by default.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .transmissionRetain(true);
```

### connectionFeedbackTopic(String topic)

The topic for broadcasting if connection is established or not.

```java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .connectionFeedbackTopic("lifecycle");
```
