v0.2.5 π¦ [T-Rex] MQTT
Pre-release
Pre-release
This new version brings MQTT support. A big thank you to @py4mac β¨
You can now use Capsule as an MQTT subscriber:
package main
import (
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
)
func main() {
hf.OnMqttMessage(Handle)
}
func Handle(params []string) {
hf.Log("π on topic: " + hf.MqttGetTopic() + ", π message" + params[0])
_, err := hf.MqttPublish("topic/reply", "it's a wasm module here")
if err != nil {
hf.Log("π‘ ouch something bad is happening")
hf.Log(err.Error())
}
}
# the topic is defined when launching the capsule launcher
capsule \
-wasm=./hello.wasm \
-mode=mqtt \
-mqttsrv=127.0.0.1:1883 \
-topic=topic/sensor0 \
-clientId=sensor
And you can use Capsule as an MQTT publisher:
package main
import (
"errors"
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
"strings"
)
func main() {
hf.SetHandle(Handle)
}
func Handle(params []string) (string, error) {
var errs []string
// a new connection is created at every call/publish
_, err1stMsg := hf.MqttConnectPublish("127.0.0.1:1883", "sensor_id0", "topic/sensor0", "π Hello from WASM with MQTT π")
_, err2ndMsg := hf.MqttConnectPublish("127.0.0.1:1883", "sensor_id0", "topic/sensor1", "π Hello World π")
if err1stMsg != nil {
errs = append(errs, err1stMsg.Error())
}
if err2ndMsg != nil {
errs = append(errs, err2ndMsg.Error())
}
return "MQTT Rocks!", errors.New(strings.Join(errs, "|"))
}
capsule \
-wasm=./hello.wasm \
-mode=cli