This page shows practical MQTT patterns in Rye:
Prerequisites
Publishes a single message to a test topic. Useful to sanity-check network access and DNS.
; Publish a message to a public broker topic
print "Connecting to public MQTT broker"
client: Open mqtt://test.mosquitto.org:1883/rye-test-p
|^check "couldn't connect to MQTT"
^ensure client .Is-connected "Not really connected"
client .Publish-simple topic: "rye/test" "Hello from Rye!"
|^check "failed to send the message"
print2 "Message sent to: " topic
client .Disconnect |^check "failed to disconnect"
rye/test if needed.Publish-simple) to keep the snippet short.Subscribes to a topic and prints received messages. This example keeps running.
client: Open mqtt://test.mosquitto.org:1883/rye-test
|^check "couldn't connect to MQTT"
^ensure client .Is-connected "Not really connected"
handler: fn { txt msg } {
print "\n-NEW-MESSAGE----" ,
print txt , print ""
print [ "Topic:" msg."topic" ]
print [ "QoS:" msg."qos" ]
print [ "Retained:" msg."retained" ]
}
topic: "rye/test"
client .Subscribe topic 1 ?handler
print "listening ..."
select { } ; keep running
This self-contained demo connects to a local broker, subscribes to a topic, then publishes a few messages. If MQTT isn’t built in your binary, it prints a friendly message and exits successfully.
; MQTT publish/subscribe demo (guarded). Requires local broker at mqtt://localhost:1883
private { mqtt-connect } |type? |= 'word
|either {
cli: mqtt-connect { url: "mqtt://localhost:1883" client-id: "rye-demo" }
topic: "rye/demo"
; subscribe with a simple handler
mqtt-subscribe cli topic { msg |
print join { "recv: " (msg -> "payload" |->string) }
}
; publish a few messages
mqtt-publish cli topic "hello"
mqtt-publish cli topic "world"
sleep-ms 100
mqtt-disconnect cli
} {
print "MQTT not available in this build."
}
Tips
When connecting to a remote service, prefer returning-words that add context and exit on errors:
client: Open mqtt://broker:1883/my-client |^check "connect failed"
^ensure client .Is-connected "not connected"
client .Publish-simple topic: "app/heartbeat" "ok" |^check "publish failed"
client .Disconnect |^check "disconnect failed"
|^check adds context to the error and returns from the function/main script.^ensure converts a falsy value into a failure and returns.For small tools, parse arguments and apply a consistent error style:
args: rye .Args? ; argv as a Rye block
spec: {
program "mqtt-pub"
-b|broker string optional "mqtt://localhost:1883"
-t|topic string required
-m|msg string required
}
res: parse-args args spec |^fix { print generate-help spec , .format-parse-errors .print , exit 1 }
client: Open res."broker"/rye-pub |^check "connect failed"
^ensure is-positive client .Is-connected "not connected"
client .Publish-simple topic: res."topic" res."msg" |^check "publish failed"
client .Disconnect |^check "disconnect failed"
examples/mqtt/publisher.rye and examples/mqtt/subscriber.ryeexamples/batteries/mqtt_02_publish_subscribe.rye