IN THIS ARTICLE
We’ve updated our SDKs, and this code is now deprecated.
Good news is we’ve written a comprehensive guide to building a multiplayer game. Check it out!
In our Multiplayer Game App Challenge, full stack developer Thomas Hunter built Robot Onslaught, a twin-stick multiplayer robot-shooting game.
Robot Onslaught used a number of different technologies to run entirely serverless, with PubNub sitting as the network transport layer, to ensure reliable and consistent peer-to-peer lockstep for user position. PubNub keeps data synchronized between the two (or any number of) clients.
We say, cool! Robot Onslaught is a great use case for where PubNub sits in multiplayer game development and communications. Let’s get to the action. You can play Robot Onslaught here. Open two browsers and shoot yourself, or force a friend to play with you and shoot your friend. Either way works.
You can also check out the full GitHub repo here, and we recommend also taking a look at Phaser.io, a game engine for HTML5 games.
Multiplayer Peer-to-Peer Lockstep Communication with PubNub
In the simplest terms, when a user say moves their player on the screen, that player’s movement needs to be reflected in realtime across every other user’s screen. So how do we do that?
You need to pass movements back and forth over PubNub through a private channel unique to each game, and animate those moves received from the remote opponent(s). One might think that the best way to do this is to send the entire state of the world to every other user whenever a state changes (ie. a user takes an action).
This is a recipe for lag. Instead, using PubNub, you can simply send an add/remove command to a channel everytime a user carries out an action. We have a multiplayer peer-to-peer lockstep tutorial and demo available here.