5d-diplomacy-with-multiverse-time-travel icon indicating copy to clipboard operation
5d-diplomacy-with-multiverse-time-travel copied to clipboard

Websockets instead of polling

Open Redex68 opened this issue 2 weeks ago • 0 comments

Use WebSockets to fetch the game state instead of actively polling for changes. The server sends an update to all listeners whenever adjudication is finished. Benefits:

  • Reduced network traffic and load on backend (data is only sent when an update occurs)
  • Reduced load on the database (when adjudication finishes an event is sent out that contains the world state, that state is then sent to every client without additional database querries)

I had started work on this before the merge of the iteration fix, so this is a bit redundant since that already fixes most of the problem, but I feel like this is a bit of an upgrade on top of that.

Tl;dr on how it works: added new endpoint to the backend GET game/{gameId}/ws that receives a WebSocket connection and registers it at the WebSocketConnectionManager. Whenever the Adjudicate() method finishes, an event is invoked that runs WebSocketConnectionManager.SendUpdatedWorldData(World world) which goes through all active WebSocket connections and sends the updated world data.

On the frontend side, what was previously isRefetching is now replaced with isWaitingForUpdate which is set to true when submit is clicked and to false when the websocket gets a reply.

Redex68 avatar Feb 14 '25 16:02 Redex68