Skip to content

WebRTC · Networking

Peer to Peer Games.

A tank battle that’s actually quite fun, and it demonstrates direct communications between browsers on the same network, without the need for a game server (once the game begins).

For security reasons, browsers are not allowed to scan the network to find each other. So Laravel introduces them with a four-letter code, passes each browser the other’s network address, then gets out of the way. After that the browsers talk directly.

So how do you do it? Someone is a "host" which means they click the "Host a Game" button. They tell their friends what the room code is. They enter the room code under "Join" and click "Join". Host can see their friends entering the room. When everyone is in, the host starts the game and every browser goes full screen: arrow keys drive, Space fires. The host sets the house rules: tank and shell speed, reload, explosive, rubber or long-lasting shells, brick, electric or trench walls, a maze that moves, and robots that hunt everyone or fight on your side.

Set up a game

Join

Type the four letters the host sees.

Host

Someone has to host first. Open a room and get a code for the others.

The handshake, live

  1. 1 · Room

    The host asks Laravel for a code.

  2. 2 · Offer

    A guest posts its network addresses to the code.

  3. 3 · Answer

    The host posts its own back.

  4. 4 · Direct

    Browser to browser. The server is done.

Server requests

0

Direct messages

0

While a room is open, the host asks the server once a second whether anyone new has knocked. Game data never goes there.

  1. Host or join a game to see the handshake.

How it works

No game server, just an introduction.

01

Browsers can’t find each other

A web page can’t open a network socket, broadcast on the Wi-Fi or scan for devices, and a browser extension can’t either. That keeps every site you visit out of your home network. It also means the first hello needs a go-between.

02

Laravel makes the introduction

The host gets a room code. A guest posts a WebRTC offer to it: a short text with every network address its browser found. The host posts an answer the same way. Laravel keeps each one in its cache for a few minutes, under an atomic lock so two players joining at the same instant can’t overwrite each other, and never reads them.

03

Then they talk directly

Each guest opens two data channels to the host. ctl is reliable and ordered, for things that must arrive exactly once: a hit, a score, a new round. fast is unordered and never resends, for positions: one that arrives late is already wrong, and the next one replaces it. In Tank Maze the host’s browser runs the battle: guests send only the keys they hold, and get the whole battlefield back 30 times a second. Every browser builds the same maze from one shared number, so the map never crosses the network.

04

On one network, it stays there

On the same Wi-Fi the browsers use their local addresses, so the traffic never leaves the building and a round trip takes a few milliseconds. A public STUN server tells each browser its internet address as well, so players on different networks can often connect too. Guest and hotel Wi-Fi usually keep devices apart, and then there’s no direct path.

One code, then straight across the room.

Room codes, offers and answers stay on this server for 10 minutes at most. They hold your device’s network addresses (most browsers hide the local one behind a random .local name) and the name you typed, nothing else. Game data never reaches the server.