MQTT for Unity V1.1.0

Andreas Vogler

Introduction

"MQTT for Unity" is a powerful Unity Package designed to seamlessly integrate MQTT (Message Queuing Telemetry Transport) functionality into Unity projects, offering a user-friendly and efficient solution for enabling real-time communication and data exchange within Unity applications. This package simplifies the process of implementing MQTT protocol, allowing Unity developers to effortlessly connect their projects to MQTT broker servers and create interactive and responsive applications.

Connection

Please also take a look at the SampleScene in the scene folder.

Prefab

The MqttClient Prefab consists of three components:

If you only build your application for Non-WebGL, then you can also create your own game object and only add the "MqttClientNet" and "MqttClient" component to it.

Drag and drop the Prefab "MqttClient" to your scene.

Configuration

The "MqttClient" component holds the configuration for the MQTT connection. You can set it in the Inspector.

Configuration Options:

State

If you run the scene then you will see the current state in the Inspector.

Birth and Last Will

A birth message is sent everytime when the connection to the broker goes up. A last will message (testament message) is sent when the program goes down unintended (program kill, unintended lost connection). This message is sent by the broker. You can also define that the last will message is sent when the client disconnects the session normally - set the property "Will On Disconnect" to true. If Topic or Message is empty, then no birth or last will messages are sent.

Unity Events

Following events available:

Add your own game object with a function to it, to execute your own business logic when events are fired.

Example OnMessageArrived function, which can be added to the On Message Arrived event:

public void OnMessageArrived(MqttMessage m) { switch (m.GetTopic()) { case "Rocworks/Cube/Position/X": _positionX = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; case "Rocworks/Cube/Position/Y": _positionY = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; case "Rocworks/Cube/Position/Z": _positionZ = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; case "Rocworks/Cube/Rotation/X": _rotationX = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; case "Rocworks/Cube/Rotation/Y": _rotationY = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; case "Rocworks/Cube/Rotation/Z": _rotationZ = float.Parse(m.GetString(), CultureInfo.InvariantCulture); break; } }

Subscribe

In the Inspector you can add topics to which you want to subscribe. This topics will be automatically get subscribed when the MQTT connection gets up. You can add here as many topics you need. With the QOS property you can set the Quality of Service for the subscription.

You can also use the Subscribe function to subscribe to values. The function is available at the "Connection" item of the MqttClient component ( MqttClient.Connection.Subscribe).

public void Subscribe(string topic, int qos);

Publish

There are two functions available to publish values to the MQTT broker. One uses a byte array as value and one uses a string as value. The functions are available at the "Connection" item of the MqttClient component (MqttClient.Connection.Publish).

public void Publish(string topic, string payload, int qos = 0, bool retain = false); public void Publish(string topic, byte[] payload, int qos = 0, bool retain = false);

Example:

public class MqttDemoUnity : MonoBehaviour { public MqttClient Client; // drag your MqttClient instance to here public void PublishMyValueToMqtt(float value) { Client.Connection.Publish("Rocworks/Cube/Position/X", value.ToString(CultureInfo.InvariantCulture)); } ...