movex
movex copied to clipboard
[Master/Client Connection] Optimize the `resyncLocalState` on ack response
What
Currently, when dispatching an Action (i.e. "emitActionDispatch" event), the following steps take place:
- the Client calculates the next checked state and renders it
- the Client sends the
emitActionDispatch
& waits for the Master's ack response with the calculatedstate checksum
- once received it checks it against the local
state checksum
& in case they don't match:
- sends another call to the Master asking for the latest Master state, in order to sync it.
- waits for the master's response before it renders it to the UI
But these steps can be reduced a bit like so:
- the Client calculates the next checked state and renders it, as before
- the Client sends the
emitActionDispatch
event including thelocal Checksum
in its payload and the matching get's calculated on the Master now - The Master always returns the next
checked state
, not just the checksum. - The client verifies the 2 checksums match, and in case they don't match, it simply replaces the local one with the master one already present in the
emitActionDispatch
ack response. No need to send any other requests and wait for a response
This simplifies the flow while also not creating more traffic jam, because the client eagerly renders the local state, therefore in case of a mismatch, the fresh state renders is even faster.
Bonus
An extra optimization is possible, where the server checks the 2 checksums and only sends the next state in case there is a diff.
Why
To optimize the network traffic and simplify the flow