Skip to content

v0.2.3 🐢 [turtle]

Pre-release
Pre-release
Compare
Choose a tag to compare
@k33g k33g released this 23 Sep 05:27
· 257 commits to main since this release

Request and Reply

A NATS "publisher" can make a request to a NATS "subscriber" and wait for an answer

package main

import (
	"errors"
	hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
	"strings"
)

func main() {
	hf.SetHandle(Handle)
}

func Handle(params []string) (string, error) {

	// Publish and wait for an answer; 1 is the timeout in seconds
	res, err := hf.NatsConnectRequest("nats.devsecops.fun:4222", "notify", "👋 Hello World 🌍", 1)

	if err != nil {
		hf.Log("🔴" + err.Error())
	} else {
        // Display the answer
		hf.Log("🔵" + res)
	}

	return "NATS Rocks!", err
}

A NATS "subscriber" can reply to a request received on its subject

package main

import (
	hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
)

func main() {
	hf.OnNatsMessage(Handle)
}

func Handle(params []string) {

	hf.Log("Message on subject: " + hf.NatsGetSubject() + ", 🎉 message: " + params[0])

	// reply to the message on the current subject; 10 is the timeout in seconds
	_, _ = hf.NatsReply("Hey! What's up", 10)

}