Home > database >  Blazor webassembly: synchronize client and server time
Blazor webassembly: synchronize client and server time

Time:08-01

I created a Blazor WebAssembly app. My project contains an API (asp net core) and the Blazor WA app. The project calculate the time it takes for a team to run a track. I safe the start time in the API (server time) and I show the chronometer going on on the blazor app by doing currentTime (client time) - startTime (server time). I tested everything home without any problem but with other devices, the client time is not synced with the server time so the chronometer began at 50 seconds.

What's the best and easiest solution? (I don't need the time to be perfect)

Thanks a lot

CodePudding user response:

If the client is using an initial starting time to display a timer on the screen, then it needs to be accurate when the stopping time occurs as well. Why not have the client poll for an updated chronometer state once per second while the client is open? Every second the client gets from the server the start time, the elapsed time since, and the end time (if one exists).

Alternatively, you could do the same thing with SignalR if you want the stop pushed from the server and not from client polling (would save some bandwidth and make the timer more accurate at the cost of complexity in the page).

  • Related