Skip to content

v0.2.5 πŸ¦– [T-Rex] MQTT

Pre-release
Pre-release
Compare
Choose a tag to compare
@k33g k33g released this 25 Sep 06:03
· 238 commits to main since this 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