'What's the difference between device and device-memory in pubsub?
as written in the toit standard library documentation for pub-sub (see https://libs.toit.io/pubsub/library-summary) there are two device topics. One of them is device:* and the other one device-memory:*.
I couldn't find any explanation what the difference would be... Any idea?
Thanks!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Florian Loitsch |
