I’m working on a web-based multiplayer online game using HTML, JavaScript, and CSS instead of Flash. When players first load the game, we download JavaScript and CSS files and make several AJAX requests. The page stays loaded until the user manually refreshes it.
We store player data like money, experience points, and other stats in a JavaScript object. This data changes when players perform actions, but here’s the tricky part - other players can also modify your data (like during battles).
We’ve tried two approaches so far:
Method 1: Every time a player clicks on tabs or performs actions (fighting, purchasing items), we make an AJAX call to fetch fresh data from the database.
Method 2: Same as method 1, but we added a proxy layer to check if the data actually changed before hitting the database.
The problem is that method 1 results in about 70% unnecessary database queries, while method 2 still has 70% unnecessary AJAX calls.
Currently using method 2, but I’m wondering if there’s a more efficient way to handle this situation. What would be the best approach to minimize these wasteful requests?
By unnecessary AJAX calls, I mean requests made when the player data hasn’t actually changed between calls.
Try a versioning system with delta updates. Don’t fetch complete player data every time - assign version numbers to your data objects instead. When requesting updates, send the current version number to your server. The server responds with only the changes since that version, or says ‘no changes’ if you’re current. This cuts bandwidth and processing overhead dramatically. I built something similar for a trading app where user portfolios needed constant sync. The key was treating updates as incremental patches, not full replacements. For your multiplayer game, add selective subscriptions so players only get updates for data that’s actually relevant to them right now. If someone’s not in combat, they don’t need real-time battle updates. This contextual filtering cuts unnecessary communication while keeping data consistent.
websockets are def the way to go! u dont need all that polling - let the server push updates when things change. I used this in similar proj and it cut down on a lot of wasted requests. Plus, you can batch updates if they come fast, way better than ajax.
Try a hash-based approach - send a hash of your current player state with each request instead of fetching everything. Server only sends updates when hashes don’t match. What’s causing most of these data changes tho? If they’re predictable events rather than random, you could optimize even more by knowing the patterns.