MQTT Connector
The MQTT Connector component provides MQTT protocol integration with automatic event routing and state management.
Features
- Automatic connection management
- Event-based message handling
- QoS level support
- Topic subscription management
- Secure connection options
XAML Configuration
<MQTTConnector Name="MQTTConnector"
Path=":mqtt"
Host="{Binding MQTTHost}"
Port="{Binding MQTTPort}"
Username="{Binding MQTTUser}"
Password="{Binding MQTTPassword}"
QoS="1"
onConnect="handleConnect"
onMessage="handleMessage"
onError="handleError">
<!-- Child components receive MQTT messages after processing -->
<ChildComponent />
</MQTTConnector>Implementation
class MQTTConnector extends CoreElement {
private client: mqtt.Client;
constructor() {
super();
this.properties.set("host", "localhost");
this.properties.set("port", 1883);
this.properties.set("qos", 1);
}
async connect(): Promise<void> {
this.client = mqtt.connect({
host: this.properties.get("host"),
port: this.properties.get("port"),
username: this.properties.get("username"),
password: this.properties.get("password")
});
this.client.on("message", async (topic, message) => {
const event = new MiddlewareEvent("message", {
topic,
message: message.toString()
});
await this.routeEvent(event);
});
}
async handleEvent(event: MiddlewareEvent): Promise<void> {
if (event.type === "publish") {
const { topic, message } = event.data;
this.client.publish(topic, message);
}
}
}WPF Integration
In WPF, the component provides a visual interface for:
- Connection status monitoring
- Message inspection
- Topic subscription management
- QoS configuration
Best Practices
-
Connection Management
- Use secure connections in production
- Implement reconnection logic
- Monitor connection state
-
Message Handling
- Process messages asynchronously
- Implement error handling
- Use appropriate QoS levels
-
Topic Design
- Use hierarchical topics
- Follow naming conventions
- Consider wildcards carefully