Asynchronous Multiplayer Development for Mobile Games
Async multiplayer—turn-based variant where both players are not online simultaneously. Classic examples: Words with Friends, Clash Royale Trophy Road with replay attacks, any "move per day" format. Player makes move, data goes to server, opponent logs in hours later and sees updated state.
State Storage and Versioning
Each move—database transaction. Record structure: game_id, move_number, player_id, action_payload (JSON), timestamp, resulting_state_hash. State hash after each move detects divergence: client recalculates hash and compares with server. If mismatch—request full snapshot.
Event sourcing works better than just storing current state: can restore any game moment, implement replay, audit, and rollback on logic bugs.
Sync via Polling and WebSocket
On session start, client does GET /game/{id}/state and gets current state with version number. Two notification variants: polling every 30-60 sec or WebSocket/SSE when app open + push when closed.
Polling simpler but loads server. Better: WebSocket on open with game_state_updated events. When closed—FCM/APNs push. On push receive—client opens game and does GET /game/{id}/state?since_version={lastKnown}—gets only changes since last known version.
Offline-First UX
User moves without internet—move saves locally and sends when connection recovers. On Android—WorkManager with NetworkType.CONNECTED: task executes when internet available, even if app closed.
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val submitMoveRequest = OneTimeWorkRequestBuilder<SubmitMoveWorker>()
.setConstraints(constraints)
.setInputData(workDataOf("move_payload" to moveJson))
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
.build()
WorkManager.getInstance(context).enqueue(submitMoveRequest)
On iOS—BGProcessingTask with requiresNetworkConnectivity = true. Move saves in Core Data, task sends at first opportunity.
Conflicts: if both players somehow send move simultaneously (client bug), server accepts only first by timestamp and returns error to second with current state.
Timeline
Basic async system for 2 players with event sourcing, offline-first, and push notifications: 2-4 weeks. Cost calculated individually after game mechanic analysis.







