MQTT client for D
MQTT broker client library written completely in D.
MQTT protocol version supported: 3.1.1
Depends on: vibe.d
Tested on:
Supported MQTT 3.1.1 features:
Pull Requests are welcome, don’t be shy ;)
Example code can be found in the examples
directory.
Simple publisher which connects to the MQTT broker and periodically sends a message.
Implicitly it connects to 127.0.0.1:1883
auto settings = Settings();
settings.clientId = "test publisher";
auto mqtt = new MqttClient(settings);
mqtt.connect();
auto publisher = runTask(() {
while (mqtt.connected) {
mqtt.publish("chat", "I'm still here!!!");
sleep(2.seconds());
}
});
Simple subscriber which connects to the MQTT broker, subscribes to the topic and outputs each received message.
Implicitly it connects to 127.0.0.1:1883
auto settings = Settings();
settings.clientId = "test subscriber";
settings.onConnAck = (scope MqttClient ctx, in ConnAck packet)
{
ctx.subscribe(["chat"]);
};
settings.onPublish = (scope MqttClient ctx, in Publish packet)
{
writeln("chat: ", cast(string)packet.payload);
};
auto mqtt = new MqttClient(settings);
mqtt.connect();