0

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!

R_B_
  • 55
  • 6

1 Answers1

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.

Florian Loitsch
  • 7,698
  • 25
  • 30
  • Thanks a lot for your answer! In case I'm not acknowledging the messages, will they still be pending in the queue as well? – R_B_ May 24 '22 at 10:42
  • I haven't done any tests, but it should. Note that some functions have the auto-ack flag set to true by default. – Florian Loitsch May 24 '22 at 14:41