Live data for games

I’m making a kind of slow-paced game and thinking of ways of syncing multiple devices.

I could do that by having all the devices repeatedly querying a database for updates in the game state. This isn’t ideal, because using too much SQL bandwidth to my understanding can get you suspended. Is there any alternative less bandwidth-heavy and elegant solution which could work on free hosting?

Ideally, you don’t poll the server for updates from the devices. Like you said, having to spin up a PHP script, query the database and return the information that nothing has changed isn’t ideal.

The “right” way could be to use Websockets. That way, the device maintains a persistent connection to a server, so the server can push messages to the client.

However, our hosting doesn’t support hosting websockets (and you shouldn’t do that with PHP in general). But you could use an external websocket hosting service like Pusher to handle that part.

Still, maintaining those connections isn’t free. So it depends on how “slow-paced” your game is to say whether that’s feasible.

8 Likes

Hey, there’s a lot that goes on in the network to properly keep a multiplayer game in-sync. A minor network glitch can cause the devices to go out of sync. The best strategy would be to implement a client-server approach where the server servers as a single source of truth, maintains the game state and publishes that at a set interval to all the players.

As others have suggested, the best way to implement this would be to use WebSockets, which gives you a bi-directional and persistent connection allowing the server and the players to always keep in touch and get updates in a matter of milliseconds.

I’ve discussed this in great detail in my article on Dev.to: Evaluating networking protocols for realtime multiplayer games. It also has code examples with a link to the [GitHub repo](http://Evaluating networking protocols for realtime apps). I hope it’s helpful!