I have a model for an app that is fairly large, all of which needs to be on a users’ device at the same time. For example, a spreadsheet. If a user makes a change to the model on one device, I want this change to propagate to all other users, via updating a centralised server.
I’m not quite sure the best way to achieve this. I have a couple of ideas:
- Each change of the model results in notifying the server of the change, where it updates it’s database, and the change event is emitted via a socket to all connected devices. Each device then updates it’s model when it receives the message from the server.
My worry with this option is that if something goes wrong on the server and the event is not emitted, we lose consistency.
- Each change of the model results in notifying the server of the change, where it updates it’s database. It then tells all devices that they will need to update their model. Devices then pull a new copy of the whole model from the server.
Here there may be performance issues associated with downloading large amounts of data to devices repeatedly.
I am wondering what an approach to addressing this issue might be?
What you suggest is the usual way to achieve this – the alternative is to directly communicate with the other devices in a per-to-peer topology, but that breaks down quickly as devices are added.
So your requirement is to check and detect errors. You could implement a heartbeat to the server so you know its alive, and/or you can add a version number that is incremented every time a device sends an update, then other devices can tell if an update is late or if they’ve missed an update or two (and then force the user to re-sync)
You should be sending just the changes required from the server to the devices, in much the same way as each device sends a delta change to the server. With big documents the propagation time is likely to be prohibitive – a device may not be able to keep up with the rate of changes that are being sent otherwise (think a game where player positions are sent as change packets, not the entire game world downloaded each frame)