Overview
A single-page chess game written in plain HTML, CSS, and JavaScript. One player hosts and shares a link; the other joins. Moves and chat messages travel directly between the two browsers over a PeerJS (WebRTC) data connection.
The chess rules — including castling, en passant, promotion, checkmate, and stalemate — are implemented from scratch without a chess library.
The idea
Most online chess requires accounts, servers, and a database. This project asks a smaller question: can two people play a full game of chess from a single web page, connected directly browser to browser?
How it works
No application server: two browsers talk directly once connected.
5 layers · select a layer for details
-
Host Player 1 creates a game
PeerJS assigns a peer ID and the host shares an invite link.
- PeerJS
-
Signalling PeerJS signalling service
Brokers the initial connection between the two browsers.
- WebRTC
-
Guest Player 2 joins via the link
Connects to the host's peer ID and opens a data connection.
-
Game Rules engine in the browser
Legal-move generation, special moves, and checkmate/stalemate detection in vanilla JavaScript.
- JavaScript
-
Sync Moves & chat over the data channel
Each move is sent to the opponent and applied to their board.
Connecting two browsers
The game uses PeerJS, a library on top of WebRTC. One player hosts a game and shares an invite link; the other opens it and joins using the host's peer ID. PeerJS's signalling service brokers the initial connection, after which moves and chat messages travel over a WebRTC data connection between the two browsers. There is no application backend and no database.
A rules engine in plain JavaScript
Move validation is written from scratch, without a chess library:
- Pseudo-legal moves first. Each piece generates its candidate moves.
- Then legality. Any move that would leave the player's own king under attack is discarded, using an "is this square attacked?" check.
- Special moves. Castling (with castling rights tracked through the game), en passant, and pawn promotion with a choice of piece.
- Game end. Checkmate and stalemate are detected when a player has no legal moves.
Keeping both boards in sync
Each move is sent to the opponent over the data connection and applied to their board, so both players see the same position. The same channel carries in-game chat messages. Moves can be made by clicking or by drag-and-drop.
Limitations
- Restrictive networks. Peer-to-peer connections can fail where WebRTC can't traverse NAT without a relay (TURN) server.
- No persistence. With no database, refreshing the page ends the game.
- Third-party signalling. Connection setup relies on the PeerJS public signalling service.