본문으로 건너뛰기

MQTT

Overview

PortDIC supports MQTT in two complementary modes:

ModeInterfaceUse Case
Embedded BrokerMQTT via Port.BroadCast<MQTT>()Run an MQTT broker inside your process — no external server needed
Client HandlerIMQTTHandler via [MQTTHandler] / [MQTTHandlerProp]Connect to any external MQTT broker; pub/sub with full event callbacks

Embedded Broker Mode

The MQTT broadcast handler starts an embedded MQTT broker inside the PortDIC process. Configure it with PortApplication.CreateBuilder() and obtain it via Port.BroadCast<MQTT>().

Quick Setup

using Portdic;

var config = PortApplication.CreateBuilder()
.New(BroadcastType.MQTT)
.MqttAddress("127.0.0.1:8080")
.Users(new User("admin", "admin"))
.AllowRemotes("127.0.0.1")
.UseMqttProtocol(false) // false = WebSocket, true = native MQTT/TCP
.Build();

var mqtt = Port.BroadCast<MQTT>();
Port.Run();

Multi-User Production Setup

var config = PortApplication.CreateBuilder()
.New(BroadcastType.MQTT)
.MqttAddress("0.0.0.0:1883")
.Users(
new User("admin", "admin123"),
new User("sensor01", "sensor_pass"),
new User("client_app", "app_secret")
)
.AllowRemotes("192.168.1.0/24", "10.0.0.100")
.UseMqttProtocol(true) // native MQTT over TCP
.Build();

var mqtt = Port.BroadCast<MQTT>();
Port.Run();

Broker Configuration Reference

MethodTypeDefaultDescription
MqttAddress(address)string"127.0.0.1:8080"Broker listen address in host:port format
Users(params User[])User[](required)Authorized users. First entry is the master user
AllowRemotes(params string[])string[](required)Allowed client IPs, hostnames, or CIDR ranges
UseMqttProtocol(bool)booltruetrue = native MQTT/TCP; false = MQTT over WebSocket

User class

new User("username", "password")
new User() // defaults: name="admin", password="admin"

Transport Modes

UseMqttProtocolTransportTypical PortUse Case
trueNative MQTT over TCP1883 / 8883MQTT clients, edge devices
falseMQTT over WebSocket8080Browser-based clients, dashboards

Monitoring with MQTT Explorer

MQTT Explorer provides a visual interface to inspect topics and messages in real time.

FieldValue
Host127.0.0.1
Port8080 (WebSocket) or 1883 (MQTT)
Usernameadmin
Passwordadmin

MQTT Explorer Login

MQTT Live View

Publication File

Create a .pub file to declare which PortDIC data keys are published to MQTT topics:

File path: app/mqtt/room1.pub

room1 RoomTemp1 // [group-name] [message-name]
room2 RoomTemp1

Client Handler Mode

The handler pattern creates one independent MQTT client per class registration. This mode is backed by portmqttclient.dll (Rust FFI) and supports publish, subscribe, and full connection event callbacks.

1. Define an MQTT client class

using Portdic;
using Portdic.MQTT;

[MQTTHandler]
public class SensorClient
{
[MQTTHandlerProp]
public IMQTTHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetHost("192.168.1.50");
handler.SetPort(1883);
handler.SetClientId("sensor-client-01");
handler.SetUsername("user");
handler.SetPassword("pass");

handler.OnMessageReceived += OnMessage;
handler.OnEvent += OnEvent;
}

private void OnMessage(string name, string topic, string payload, int qos)
{
Console.WriteLine($"[{name}] {topic}: {payload} (QoS {qos})");
}

private void OnEvent(string name, string eventType, string description)
{
Console.WriteLine($"[{name}] {eventType}: {description}");
if (eventType == "CONNECTED")
{
handler.Subscribe("sensor/temperature/#");
handler.Subscribe("sensor/humidity", qos: 1);
}
}
}

2. Register and run

Port.Add<SensorClient>("mqtt_sensor");
Port.Run();

Multiple broker connections

Each registration creates its own independent MQTT client:

Port.Add<LocalBrokerClient>("mqtt_local");
Port.Add<CloudBrokerClient>("mqtt_cloud");
Port.Run();

Publish and Subscribe

// Subscribe to a topic
handler.Subscribe("equipment/status");
handler.Subscribe("sensor/#", qos: 1); // wildcard, QoS 1

// Publish a message
handler.Publish("equipment/status", "online");
handler.Publish("sensor/temp", "25.3", qos: 1, retain: true);

// Unsubscribe
handler.Unsubscribe("sensor/#");

// Disconnect
handler.Close();

QoS Levels

LevelGuaranteeDescription
QoS 0At most onceNo delivery guarantee; possible message loss
QoS 1At least onceGuaranteed delivery; possible duplication
QoS 2Exactly onceGuaranteed delivery without duplication

API Reference

Attributes

AttributeTargetDescription
[MQTTHandler]ClassMarks the class as an MQTT client handler container
[MQTTHandlerProp]PropertyInjects the IMQTTHandler instance
[Preset]MethodCalled before Open() to configure the handler

Configuration methods

MethodDescription
SetHost(string host)Broker address. Default: "127.0.0.1"
SetPort(int port)Broker port. Default: 1883
SetClientId(string clientId)MQTT client ID. Defaults to the registration key if empty
SetUsername(string username)Broker authentication username
SetPassword(string password)Broker authentication password
SetKeepAlive(int seconds)Keep-alive interval in seconds. Default: 60
SetCleanSession(bool)Discard previous session state on connect. Default: true

Connection methods

MethodReturnsDescription
Open()ERROR_CODEConnect to the MQTT broker
Close()ERROR_CODEDisconnect from the broker
IsConnectedbooltrue if currently connected to the broker

Publish / Subscribe methods

MethodReturnsDescription
Publish(topic, payload, qos, retain)intPublish a message. Returns 0 on success
Subscribe(topic, qos)intSubscribe to a topic filter. Returns 0 on success
Unsubscribe(topic)intUnsubscribe from a topic. Returns 0 on success

Logging

MethodDescription
SetLogger(string rootPath)Enable hourly-rotated log files. Format: mqtt_{name}_{date}_{hour}.log
WriteLog(string message)Write a custom entry to the log file

Events

OnMessageReceived

Fired when a message arrives on a subscribed topic.

handler.OnMessageReceived += (string name, string topic, string payload, int qos) =>
{
Console.WriteLine($"[{name}] {topic}: {payload}");
};
ParameterDescription
nameClient registration key
topicTopic the message was published on
payloadMessage payload as a UTF-8 string
qosQoS level (0, 1, or 2)

OnEvent

Fired on connection state changes and errors.

handler.OnEvent += (string name, string eventType, string description) =>
{
Console.WriteLine($"[{name}] {eventType}: {description}");
};
eventTypeTriggered When
CONNECTEDConnected to the broker
DISCONNECTEDDisconnected from the broker
SUBSCRIBEDTopic subscription confirmed
UNSUBSCRIBEDTopic unsubscription confirmed
PUBLISHEDMessage published successfully
ERRORError occurred (description contains detail)

Error Codes

CodeValueMeaning
ERR_CODE_NO_ERROR1Success
ERR_CODE_OPEN-1Open/connect failed
ERR_CODE_DLL_NOT_LOADED-2portmqttclient.dll not loaded
ERR_CODE_PORTNAME_EMPTY-3Client name not set
ERR_CODE_DLL_FUNC_NOT_CONFIRM-4Required DLL function unavailable
ERR_CODE_CONNECT_FAILED-5Connection attempt failed

  • RTSP — Real-time video streaming
  • SECS/GEM — Semiconductor equipment protocol
  • TCP — Raw TCP client/server communication