Chess for Two

A browser-based two-player chess game that connects players peer-to-peer over WebRTC — no backend server or database.

Illustration of a chessboard with two browser windows connected by a peer-to-peer link.
My role
Developer
Type
Personal project
Status
Live demo

Stack

  • JavaScript
  • HTML
  • CSS
  • PeerJS
  • WebRTC
On this page
  1. Overview
  2. The idea
  3. How it works
  4. Connecting two browsers
  5. A rules engine in plain JavaScript
  6. Keeping both boards in sync
  7. Limitations

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

  1. Host Player 1 creates a game

    PeerJS assigns a peer ID and the host shares an invite link.

    • PeerJS
  2. Signalling PeerJS signalling service

    Brokers the initial connection between the two browsers.

    • WebRTC
  3. Guest Player 2 joins via the link

    Connects to the host's peer ID and opens a data connection.

  4. Game Rules engine in the browser

    Legal-move generation, special moves, and checkmate/stalemate detection in vanilla JavaScript.

    • JavaScript
  5. 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.
Abstract illustration of a clinical dashboard with intake form, schedule, and SOAP-note panels. Contains no real data. In production

Healthcare software Professional

EMR & Patient Intake Platform

A production Electronic Medical Record platform covering patient intake, clinical documentation, scheduling, billing, and practice administration.

  • Python
  • Django
  • Django REST Framework
  • JavaScript
  • and 7 more
Login I Lanexis PVT LTD - customer.lanexis.com Completed and deployed

Business platform Client project

Lanexis Customer Portal

A B2B customer portal I planned, designed, built, and deployed end to end Django backend, React Native/Expo app, Google Sheets integration, and AWS infrastructure.

  • Django
  • Django REST Framework
  • React Native
  • Expo
  • and 7 more
Abstract illustration of text messages turning into a TF-IDF matrix and an SVM decision boundary. Internship project

Machine learning Internship

SMS Spam Classification

An SVM text classifier that separates spam from legitimate SMS messages using TF-IDF features, tuned with GridSearchCV.

  • Python
  • scikit-learn
  • SVM
  • TF-IDF
  • and 2 more

→Get in touch

Working on something similar?

I’m happy to talk through the problem, the trade-offs, and how I’d approach it.