The device-memory version doesn't store the object in flash. As such it is more efficient, but it may lose data if the device is powered down before subscriptions have a chance to read the data.
For example:
// ---------- publish.toit -----------
import pubsub
main: pubsub.publish "device-memory:test" "test $Time.now"
// ---------- subscribe.toit ----------
import pubsub
main:
print "running"
sleep --ms=3_000
pubsub.subscribe TOPIC: | msg |
print "received message: $msg.payload.to_string"
// ---------- publish.yaml ----------
name: device publisher
entrypoint: publish.toit
triggers:
on_boot: true
// ---------- subscribe.yaml ----------
name: device subscriber
entrypoint: subscribe.toit
triggers:
on_boot: true
pubsub:
subscriptions:
- "device-memory:test"
If you install these programs, and reset the device you should see the following output in your logs:
received message: test 2022-05-17T09:35:30Z 11:35:33 message
0 11:35:30 process stop
running 11:35:30 message
11:35:30 process start
11:35:30 process start
Now, if you reset the device just after you see the "running", but before the receiver has a chance to actually get the data, then the published message will be lost.
However, if you switch the topic to device:test
(in all files), then the message is written to flash, and resetting the device won't lose any data.