Peer-to-Peer Lockstep for Multiplayer Communication

2 min readDec 9, 2014

Warning

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 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 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

How to Create a Dating App: 7 Steps to Fit Any Design
Insights6 minMar 15, 2023

How to Create a Dating App: 7 Steps to Fit Any Design

There are common underlying technologies for a dating app, and in this post, we’ll talk about the major technologies and designs...

Michael Carroll

Michael Carroll

How to Create a Real-time Public Transportation Schedule App
Build6 minMar 14, 2023

How to Create a Real-time Public Transportation Schedule App

How to use geohashing, JavaScript, Google Maps API, and BART API to build a real-time public transit schedule app.

Michael Carroll

Michael Carroll

How to Create Real-Time Vehicle Location Tracking App
Build2 minMar 9, 2023

How to Create Real-Time Vehicle Location Tracking App

How to track and stream real-time vehicle location on a live-updating map using EON, JavaScript, and the Mapbox API.

Michael Carroll

Michael Carroll

Talk to an expert