Real-Time Gaming Blog

Peer-to-Peer Lockstep for Multiplayer Communication

2 min read Joe Hanson on Dec 9, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

Robot Onslaught is a multiplayer game that uses PubNub to power and sync all peer-to-peer lockstep to ensure accurate, real-time player position.


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.

peer-to-peer lockstep

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, 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 real time 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.

Robot Onslaught Video Demo

More from PubNub

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff