Let's pretend that:
import pubsub
topic ::= "cloud:demo/INP"
main:
print "wakeup - checking messages"
pubsub.subscribe topic --blocking=false: | msg/pubsub.Message |
sender := ?
if msg.sender.is_device:
sender = "Device($msg.sender.hardware_id)"
else:
sender = "ExternalSystem($msg.sender.external_name)"
print "Received message '$msg.payload.to_string' from $sender"
import pubsub
INCOMING_TOPIC ::= "cloud:demo/INP"
OUTGOING_TOPIC ::= "cloud:demo/OUT"
main:
print "app is started"
pubsub.subscribe INCOMING_TOPIC --auto_acknowledge: | msg/pubsub.Message |
print "received: $msg.payload.to_string"
pubsub.publish OUTGOING_TOPIC msg.payload
print "sent: $msg.payload.to_string"
Thanks in advance for your reply
Toit PubSub does broadcasting - all devices will receive the message.
Every message sent on a topic contains a publisher
which can either be an external
or a device
.
A device
who publishes will have the hardware_id
and the job_id
which is a unique way to determine what device sent the message. to external subscribers we also send device_id
.
An external
who publishes will contain the name
which is the identifier of the external application that published the message. For example in the CLI you can write a message using: toit pubsub write <topic> <external-name> -- <msg>
here the <external-name>
will be what is filled into the publisher.external.name
property.