> For the complete documentation index, see [llms.txt](https://vc-docs.gitbook.io/engine/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vc-docs.gitbook.io/engine/home/doxygen_synced/classes/class_m_q_t_t_client.md).

# MQTTClient

**Usage:** `const c = new api.MQTTClient(); c.Connect(uri, port, id, user, pass, cert, ok => {});`

MQTT client for connecting to a broker, subscribing to topics, and publishing messages over the MQTT protocol with optional TLS encryption.

## Public Types

|            | Name                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| enum class | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#enum-servicelevel"><strong>ServiceLevel</strong></a> { AtMostOnce = MqttQualityOfServiceLevel.AtMostOnce, AtLeastOnce = MqttQualityOfServiceLevel.AtLeastOnce, ExactlyOnce = MqttQualityOfServiceLevel.ExactlyOnce}<br>Quality of Service levels that determine the delivery guarantee for MQTT messages.</p> |

## Public Functions

|         | Name                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|         | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-mqttclient"><strong>MQTTClient</strong></a>()<br>Initializes a new MQTT client. The client is created in a disconnected state; call <a href="/pages/BIKJalkTZ3jiYOS3VVVb">Connect</a> to establish a session with a broker.</p>                                                                                                                                                                                                                                 |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-connect"><strong>Connect</strong></a>(string uri, int port, string clientID, string userName, string password, byte\[] derRootCertificate, Action< bool > callback)<br>Establishes a TLS-secured connection to an MQTT broker. The method returns immediately and reports the connection result through the provided callback once the handshake completes.</p>                                                                                                 |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-disconnect"><strong>Disconnect</strong></a>()<br>Closes the connection to the MQTT broker and releases the underlying network session.</p>                                                                                                                                                                                                                                                                                                                      |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-subscribetopic"><strong>SubscribeTopic</strong></a>(string topic, <a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#enum-servicelevel">ServiceLevel</a> serviceLevel)<br>Registers interest in a topic so the client will receive messages published to it. Received messages are delivered through the handler set via <a href="/pages/BIKJalkTZ3jiYOS3VVVb">CallbackOnMessageReceived</a>.</p> |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-unsubscribetopic"><strong>UnsubscribeTopic</strong></a>(string topic)<br>Cancels an existing subscription so the client will no longer receive messages on the given topic.</p>                                                                                                                                                                                                                                                                                 |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-publishtotopic"><strong>PublishToTopic</strong></a>(string topic, string data)<br>Sends a string message to the broker on the specified topic. The broker forwards the message to all clients currently subscribed to that topic.</p>                                                                                                                                                                                                                           |
| void    | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-callbackonmessagereceived"><strong>CallbackOnMessageReceived</strong></a>(Action< string, string > callback)<br>Registers a handler that is invoked whenever a message arrives on any subscribed topic. The handler receives the topic name and the message payload as strings. Multiple handlers can be registered and all will be invoked for each incoming message. Handlers always run on the Unity main thread.</p>                                        |
| Byte\[] | <p><a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-loadderfrompem"><strong>LoadDerFromPem</strong></a>(string pem)<br>Parses a PEM-formatted certificate string and returns its raw DER-encoded bytes. Use this to convert a certificate file's text contents into the byte array format required by <a href="https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-connect">Connect</a>.</p>                                                           |

## Public Types Documentation

### enum ServiceLevel

| Enumerator  | Value                                 | Description                                                                              |
| ----------- | ------------------------------------- | ---------------------------------------------------------------------------------------- |
| AtMostOnce  | MqttQualityOfServiceLevel.AtMostOnce  | Delivers the message at most once. The message may be lost but will never be duplicated. |
| AtLeastOnce | MqttQualityOfServiceLevel.AtLeastOnce | Guarantees the message is delivered at least once. Duplicates are possible.              |
| ExactlyOnce | MqttQualityOfServiceLevel.ExactlyOnce | Guarantees the message is delivered exactly once. Slowest but most reliable.             |

Quality of Service levels that determine the delivery guarantee for MQTT messages.

## Public Functions Documentation

### function MQTTClient

```csharp
MQTTClient()
```

Initializes a new MQTT client. The client is created in a disconnected state; call [Connect](/engine/home/doxygen_synced/classes/class_m_q_t_t_client.md) to establish a session with a broker.

### function Connect

```csharp
void Connect(
    string uri,
    int port,
    string clientID,
    string userName,
    string password,
    byte[] derRootCertificate,
    Action< bool > callback
)
```

Establishes a TLS-secured connection to an MQTT broker. The method returns immediately and reports the connection result through the provided callback once the handshake completes.

**Parameters**:

* **uri** Hostname or IP address of the MQTT broker.
* **port** Port the broker is listening on (typically 8883 for TLS).
* **clientID** Unique identifier used by the broker to distinguish this client.
* **userName** Username used for authenticattion with broker. Null if no username required.
* **password** Password used for authenticattion with broker. Null if no password required.
* **derRootCertificate** DER-encoded root CA certificate used to validate the broker's TLS certificate. Null if no certificate required
* **callback** Receives `true` if the connection succeeded, `false` if it failed.

### function Disconnect

```csharp
void Disconnect()
```

Closes the connection to the MQTT broker and releases the underlying network session.

### function SubscribeTopic

```csharp
void SubscribeTopic(
    string topic,
    ServiceLevel serviceLevel
)
```

Registers interest in a topic so the client will receive messages published to it. Received messages are delivered through the handler set via [CallbackOnMessageReceived](/engine/home/doxygen_synced/classes/class_m_q_t_t_client.md).

**Parameters**:

* **topic** Topic filter to subscribe to.
* **serviceLevel** Quality of Service level the broker should use when delivering messages on this topic.

### function UnsubscribeTopic

```csharp
void UnsubscribeTopic(
    string topic
)
```

Cancels an existing subscription so the client will no longer receive messages on the given topic.

**Parameters**:

* **topic** Topic filter that was previously subscribed to.

### function PublishToTopic

```csharp
void PublishToTopic(
    string topic,
    string data
)
```

Sends a string message to the broker on the specified topic. The broker forwards the message to all clients currently subscribed to that topic.

**Parameters**:

* **topic** Topic to publish on.
* **data** String payload to send.

### function CallbackOnMessageReceived

```csharp
void CallbackOnMessageReceived(
    Action< string, string > callback
)
```

Registers a handler that is invoked whenever a message arrives on any subscribed topic. The handler receives the topic name and the message payload as strings. Multiple handlers can be registered and all will be invoked for each incoming message. Handlers always run on the Unity main thread.

**Parameters**:

* **callback** Handler called with `(topic, payload)` for every received message.

### function LoadDerFromPem

```csharp
static Byte[] LoadDerFromPem(
    string pem
)
```

Parses a PEM-formatted certificate string and returns its raw DER-encoded bytes. Use this to convert a certificate file's text contents into the byte array format required by [Connect](https://git.unimetaverse.net/vc-core-engine/doxygen/-/tree/main/Classes/Classes/class_m_q_t_t_client.md#function-connect).

**Parameters**:

* **pem** Certificate text including the `-----BEGIN CERTIFICATE-----` and `-----END CERTIFICATE-----` markers.

**Return**: The certificate's binary DER representation.

***

Updated on 2026-07-22 at 12:00:00 +0800
