How does webxdc save data?

Since it’s said that developers should use sendUpdate rather than localStorage or similar. How is the state of the app saved, considering that the app is not running all the time? Additionally, what is the recommended strategy for saving data that should not yet be sent to others in the chat?

1 Like

Both sent and received updates are saved in the profile database, in SQLite. They will not disappear until you delete the app and will be transferred if you “add second device” or make a profile backup.

You can save anything in memory if it is supposed to be stored shortly, until user closes the app. If you really need to store something locally but not send it, you can use localStorage, it works, but other devices of the user will not see it and it may be lost after some time, if user transfers profile to another device etc., so it is still a temporary storage.

Webxdc pages are “collaborative documents”, all changes are supposed to be visible to everyone at any times. If user wants to store something privately, they should use the app in the chat where only they are the member.

If you are doing a game, then maybe assuming that others are not cheating and sending an update to store something is fine.

2 Likes

Thank you, this is really cool. So if I understand correctly, when the app is opened, it replays all the updates in the database?
And the part about assuming others will not look somehow does make sense, I guess even though it might feel a little wasteful, everything can be sent as updates, especially for multi-device.

1 Like

Yes, every time you open the app, all stored updates are replayed.

There is no way to send something only to your devices but not others, so this is the only way for now.

2 Likes
let promise = window.webxdc.setUpdateListener((update) => {}, serial);

If you use the serial property, you can skip updates and not replay everything.
This can make sense if you cache the aggregated state (which you built from the updates) in local storage.

1 Like