Imagine a coin flip game whose logic looks like this:
- The server sends a websocket message (RESULT_MESSAGE) to the frontend resulting in a game (won / lost).
- The server sends a Websocket message with updated aggregated statistics (STATS_MESSAGE) to the frontend.
- The frontend responds to RESULT_MESSAGE with an animation (coin toss). In addition, the all-time statistics and the gain are updated in response to STATS_MESSAGE
The problem with this logic is that responding to STATS_MESSAGE (gain and statistic update) betrays the result of the coin toss before the animation finishes.
Solutions I can not use:
- Merging RESULT_MESSAGE and STATS_MESSAGE updates only the statistics / wins after the animation is completed. Because these 2 messages are generated and sent by different services.
- Associate a particular RESULT_MESSAGE with a STATS_MESSAGE and respond to a STATS_MESSAGE only when its corresponding RESULT_MESSAGE has been processed and its animation completed. Because in reality I have several types of STATS_MESSAGES (trophies updated, statistics updated, earnings / balance updated etc.) and it is simply not practical for the frontend to keep all these messages until their respective RESULT_MESSAGES are processed
Solutions that I've already tried, but I try to avoid:
- If the backend sends STATS_MESSAGE with a delay, it ensures that it does not reach the frontend before the coinflip animation completes. This type encodes the behavior of the frontend (animation) in the backend.
All ideas and suggestions appreciated. Many thanks!