For a minimal example, three components are needed:
- A publisher to send a message
- A server to relay the message
- A subscriber to receive the message
Normally, the broker would be started first, followed by the subscriber. Then the publisher can be executed to send the message.
serer.tcl:
tcl::tm::path add [file dirname [file normalize [info script]]]
package require broker
# Show what's happening
broker log puts
broker create server ""
server listen
vwait forever
subscriber.tcl:
tcl::tm::path add [file dirname [file normalize [info script]]]
package require mqtt
mqtt create mq
mq connect
# Receive all messages
mq subscribe # message
proc message {topic message args} {
puts "$topic: $message"
}
vwait forever
publisher.tcl:
tcl::tm::path add [file dirname [file normalize [info script]]]
package require mqtt
mqtt create mq
mq connect
mq publish minimal/example/topic "Hello, World!"
# Allow the client to connect and send the message before terminating
after 100 exit
vwait briefly