Replies: 1 comment 1 reply
-
Each piece of data that needs to be persisted and then later recalled (or deleted) is uniquely defined by the combination of: server URL, client ID, and key. So you can definitely keep everything in a singe database, and I suppose you could keep everything in a single table, but then you would need columns for all three of those items (URL, client ID, & key). I think it maps nicer to keeping each client's data in a single table, and just using the key as the index. If all the clients are talking to the same broker, then I guess you can just use the client ID as the table name. If they're talking to different brokers, maybe use a separate database for each broker, and the client ID for the table name? It's up to you. But those three items provide uniqueness. The persistence "open" callback gives you the URL and client ID:
You can use that to open/create the database and table, after which the "put" and "get" calls provide the key to use to track the individual item:
Does that all make sense? Personally I think this maps much better to a key/value store than to traditional SQL. I often use Redis for this - running on the local host, since persisting to a remote server would be ridiculous and defeat the whole purpose! I also really like Redis on embedded gateways since the data is generally kept in memory and doesn't thrash the Flash chips like writing to a file would. (Although you do need to save the current state to disk when the device powers down if you want it to persist through power cycles). With Redis I create a hash for each client/URL combination by just concatenating those two strings with a colon, ":". Then the C callbacks each map to a single Redis command:
If you're interested in further details, I have a Rust library specifically for using Redis with the Paho Rust library. The Rust lib just wraps this C library and uses the same persistence callbacks mapped through an FFI layer. But the idea is identical. Even if you don't know Rust, it's only about 100 lines, and should be reasonably understandable: |
Beta Was this translation helpful? Give feedback.
-
This is an architecture question.
If there are multiple clients connecting to a central broker - can the clients "share" the same database backing the offline messages or does each client need their own "instance" of a database for the offline published messages?
thank you!
Beta Was this translation helpful? Give feedback.
All reactions