From 9638803c59f103bd23c245fc0a561f153c3f03a1 Mon Sep 17 00:00:00 2001 From: Shashwat Agrawal Date: Thu, 30 Apr 2026 15:22:31 +0530 Subject: [PATCH 1/3] docs: add root README and document backend WebSocket API Signed-off-by: Shashwat Agrawal --- README.md | 150 ++++++++++++++++++++++++++++++++++++++++++++++ backend/README.md | 89 +++++++++++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d5eec7 --- /dev/null +++ b/README.md @@ -0,0 +1,150 @@ +# Exchange + +A real-time trading exchange system built with C++, React, and Node.js. + +## What is this? + +This project lets you run your own trading exchange on your computer. It has three parts: + +- **Backend** - The matching engine written in C++ that processes buy/sell orders +- **Frontend** - A web interface built with React to view and interact with the exchange +- **Simulator** - A tool that generates fake market data to test the exchange + +## Project Structure + +``` +exchange/ +├── backend/ # C++ matching engine (uWebSockets) +├── frontend/ # React + TypeScript web app (Vite) +└── simulator/ # Node.js market data simulator +``` + +## Prerequisites + +Before you begin, make sure you have these installed: + +- **C++ compiler** (g++ or clang++ with C++23 support) +- **CMake** (version 3.20 or higher) +- **OpenSSL** development libraries +- **Node.js** (version 18 or higher recommended) +- **npm** (comes with Node.js) + + +## Getting Started + +### Step 1: Clone and setup + +```bash +git clone https://github.com/builder-inc/exchange.git +cd exchange +git submodule update --init --recursive +``` + +This downloads the uWebSockets library that the backend needs. + +### Step 2: Build the Backend + +The backend uses secure WebSocket connections (WSS), so you need SSL certificates for local development. + +**Generate SSL certificates:** + +```bash +cd backend +mkdir -p certs +openssl req -x509 -sha256 -newkey rsa:2048 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes \ + -subj "/C=IN/O=Builder Inc./CN=localhost" \ + -addext "subjectAltName = DNS:localhost" +``` + +**Build the C++ engine:** + +```bash +cmake -S . -B build +cd build && make +``` + +**Run the test suite (optional):** +```bash +make check +``` + +**Start the backend server:** +```bash +./websocket_server +``` + +### Step 3: Install Frontend Dependencies + +```bash +cd frontend +npm install +``` + +**Start the frontend dev server:** + +```bash +npm run dev +``` + +This starts a local server. Open the URL shown in your terminal (usually http://localhost:5173). + +### Step 4: Start the Simulator + +In a new terminal: + +```bash +cd simulator +npm install +npm run dev +``` + +The simulator will start sending mock market data to test the exchange. + +## Available Commands + +### Backend + +**API Details:** See [backend/README.md](backend/README.md) for WebSocket API documentation, JSON message formats, and terminal testing tools (websocat, wscat). + +| Command | Description | +|------------------------------------------|------------------------------------------| +| `cmake -S backend -B backend/build` | Generate build files (from project root) | +| `cd backend && cmake -S . -B build` | Generate build files (from backend dir) | +| `cd backend/build && make` | Compile the engine | +| `cd backend/build && make tests` | Build the test suite | +| `cd backend/build && ./tests` | Run the test suite | +| `cd backend/build && make check` | Build and run tests in one step | +| `cd backend/build && ./websocket_server` | Start the backend WebSocket server | + +### Frontend + +| Command | Description | +|-------------------|------------------------------------------| +| `npm run dev` | Start development server with hot reload | +| `npm run build` | Create production build in `dist/` | +| `npm run lint` | Check code for style issues | +| `npm run preview` | Preview the production build locally | + +### Simulator + +| Command | Description | +|---------------|---------------------------------| +| `npm run dev` | Start the market data simulator | + +## Development Workflow + +1. Start the backend first: `cd backend/build && ./websocket_server` +2. Start the simulator: `cd simulator && npm run dev` +3. Start the frontend: `cd frontend && npm run dev` +4. Open the frontend URL in your browser + +## Troubleshooting + +**CMake can't find OpenSSL:** +Make sure libssl-dev (Debian/Ubuntu) or openssl (macOS) is installed. + +**Submodule error:** +Run `git submodule update --init --recursive` from the project root. + +**OpenSSL certificate warnings:** +The browser will show a warning for self-signed certificates. This is normal for local development. Click "Advanced" and proceed. diff --git a/backend/README.md b/backend/README.md index cd62f3f..412acd1 100644 --- a/backend/README.md +++ b/backend/README.md @@ -31,6 +31,11 @@ cd build && make cd build && cmake --build . ``` +**Start the server:** +```bash +./websocket_server +``` + ## Tests ```bash # Build test executable only @@ -46,6 +51,90 @@ make check cmake --build . --target check ``` +## API + +The server uses **WebSocket over TLS** (`wss://`) on port `7000`. Connect to `wss://localhost:7000`. + +All messages are JSON strings. Clients must authenticate before using any other command. + +### Message Types + +#### Login +Send: +```json +{"type": "login", "username": "test", "password": "test"} +``` +Response: +```json +{"type": "login_response", "status": "success"} +``` +Other statuses: `"failed"`, `"already_logged_in"` + +Default credentials: `test/test`, `nero/nero` + +#### Subscribe to Market Data +Send: +```json +{"type": "subscribe", "channel": "market/snapshot"} +``` +Response: +```json +{"type": "subscribe_response", "status": "ok", "channel": "market/snapshot"} +``` +Once subscribed, the server broadcasts a market snapshot every second. + +#### Unsubscribe +Send: +```json +{"type": "unsubscribe", "channel": "market/snapshot"} +``` +Response: +```json +{"type": "unsubscribe_response", "status": "ok", "channel": "market/snapshot"} +``` + +#### Market Snapshot (Server Broadcast) +```json +{ + "ask":{ + "level_1":0.6617469155089037,"level_2":0.5694185425603027, + "level_3":0.6623632407720937,"level_4":0.7771555433504215, + "level_5":0.9472390224056843 + }, + "bid":{ + "level_1":0.49673064124261396,"level_2":0.2642594874240395, + "level_3":0.7683473906185206,"level_4":0.6231112700637431, + "level_5":0.32603959983506586 + } +} +``` + +#### Error Responses +```json +{"type": "error", "message": "not_authenticated"} +{"type": "error", "message": "unknown_command"} +{"type": "error", "message": "invalid_json"} +{"type": "error", "message": "unknown_channel"} +``` + +### Usage Examples + +**websocat** +```bash +websocat -v --insecure wss://localhost:7000 +``` + +Then type messages directly: +```json +{"type": "login", "username": "test", "password": "test"} +{"type": "subscribe", "channel": "market/snapshot"} +``` + +**wscat**: +```bash +wscat -v -n -c wss://localhost:7000 --no-check +``` + ## Editor Notes ```bash # only needed if your editor/clangd is SHIT. From c4c8e649c23f3be2ac70af26982af3066c53be5a Mon Sep 17 00:00:00 2001 From: Shashwat Agrawal Date: Thu, 30 Apr 2026 22:42:12 +0530 Subject: [PATCH 2/3] cleanup Signed-off-by: Shashwat Agrawal --- backend/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/README.md b/backend/README.md index 412acd1..80a3fea 100644 --- a/backend/README.md +++ b/backend/README.md @@ -70,7 +70,7 @@ Response: ``` Other statuses: `"failed"`, `"already_logged_in"` -Default credentials: `test/test`, `nero/nero` +Default credentials: `test/test` #### Subscribe to Market Data Send: From 8b6fbdb492ecd7a6d6d0f6ef26a825538dfb046d Mon Sep 17 00:00:00 2001 From: Shashwat Agrawal Date: Sat, 11 Jul 2026 15:28:19 +0530 Subject: [PATCH 3/3] docs: polish root README and align API credentials with AuthManager Replace the draft overview with accurate setup, component roles, and workflow notes. Demo users match backend AuthManager (shashwat, nero) instead of the non-existent test/test account. Closes #24. --- README.md | 176 ++++++++++++++++++++++++++-------------------- backend/README.md | 6 +- 2 files changed, 103 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 5d5eec7..ed371d4 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,48 @@ # Exchange -A real-time trading exchange system built with C++, React, and Node.js. +A small real-time trading exchange playground: C++ market-data server, React UI, +and a Node prototype simulator. ## What is this? -This project lets you run your own trading exchange on your computer. It has three parts: +Three packages under one repo: -- **Backend** - The matching engine written in C++ that processes buy/sell orders -- **Frontend** - A web interface built with React to view and interact with the exchange -- **Simulator** - A tool that generates fake market data to test the exchange +| Package | Role | +|---------|------| +| **backend** | C++23 WebSocket server (uWebSockets + OpenSSL). Auth, channel subscribe, live market snapshots over `wss://`. | +| **frontend** | React + TypeScript (Vite) UI that connects to the backend, logs in, and renders the order book. | +| **simulator** | Early Node.js WebSocket mock that emits random bid/ask levels. Useful as a stand-in when the C++ server is not running. | -## Project Structure +This is not a full matching engine yet. The backend currently focuses on secure +WebSocket transport, login, and broadcast market data. Binary protocol sketches +for order entry live in `backend/TODO.md`. + +## Project layout ``` exchange/ -├── backend/ # C++ matching engine (uWebSockets) -├── frontend/ # React + TypeScript web app (Vite) -└── simulator/ # Node.js market data simulator +├── backend/ # C++ WSS server +├── frontend/ # React client +└── simulator/ # Node mock market-data server ``` ## Prerequisites -Before you begin, make sure you have these installed: +- C++23 compiler (g++ or clang++) +- CMake ≥ 3.20 +- OpenSSL + zlib development libraries +- Node.js 18+ and npm +- Git (for submodules) -- **C++ compiler** (g++ or clang++ with C++23 support) -- **CMake** (version 3.20 or higher) -- **OpenSSL** development libraries -- **Node.js** (version 18 or higher recommended) -- **npm** (comes with Node.js) +On Debian/Ubuntu: +```bash +sudo apt install build-essential cmake libssl-dev zlib1g-dev +``` -## Getting Started +## Getting started -### Step 1: Clone and setup +### 1. Clone and init submodules ```bash git clone https://github.com/builder-inc/exchange.git @@ -40,57 +50,65 @@ cd exchange git submodule update --init --recursive ``` -This downloads the uWebSockets library that the backend needs. +### 2. Backend (C++ WSS server) -### Step 2: Build the Backend +The server speaks **WebSocket over TLS** on port `7000`. -The backend uses secure WebSocket connections (WSS), so you need SSL certificates for local development. - -**Generate SSL certificates:** +Generate a local self-signed cert (dev only): ```bash cd backend mkdir -p certs -openssl req -x509 -sha256 -newkey rsa:2048 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes \ - -subj "/C=IN/O=Builder Inc./CN=localhost" \ - -addext "subjectAltName = DNS:localhost" +openssl req -x509 -sha256 -newkey rsa:2048 \ + -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes \ + -subj "/C=IN/O=Builder Inc./CN=localhost" \ + -addext "subjectAltName = DNS:localhost" ``` -**Build the C++ engine:** +Build and run: ```bash cmake -S . -B build -cd build && make +cmake --build build +cd build && ./websocket_server ``` -**Run the test suite (optional):** -```bash -make check -``` +Optional tests: -**Start the backend server:** ```bash -./websocket_server +cmake --build build --target check +# or: cd build && make tests && ./tests ``` -### Step 3: Install Frontend Dependencies +Demo logins hard-coded in the server: -```bash -cd frontend -npm install -``` +| Username | Password | +|-----------|-----------| +| `shashwat` | `shashwat` | +| `nero` | `nero` | -**Start the frontend dev server:** +WebSocket message formats, error codes, and `websocat` / `wscat` examples are in +[backend/README.md](backend/README.md). + +### 3. Frontend ```bash +cd frontend +npm install npm run dev ``` -This starts a local server. Open the URL shown in your terminal (usually http://localhost:5173). +Open the Vite URL (usually `http://localhost:5173`), connect to +`wss://localhost:7000`, log in, then subscribe to `market/snapshot`. -### Step 4: Start the Simulator +Browsers block self-signed certificates by default. Trust +`backend/certs/cert.pem` in your OS/browser, or accept the certificate warning +before the WebSocket handshake will succeed. -In a new terminal: +### 4. Simulator (optional mock) + +The simulator is a **separate** mock server on port `7000`. Do not run it at the +same time as the C++ backend — both bind the same port. ```bash cd simulator @@ -98,53 +116,59 @@ npm install npm run dev ``` -The simulator will start sending mock market data to test the exchange. +Use it when you only need a throwaway market-data stream and do not want to +build the C++ server. -## Available Commands +## Typical local workflow -### Backend +1. Generate certs and start the backend: `cd backend/build && ./websocket_server` +2. Start the frontend: `cd frontend && npm run dev` +3. In the UI: Connect → Login → Subscribe -**API Details:** See [backend/README.md](backend/README.md) for WebSocket API documentation, JSON message formats, and terminal testing tools (websocat, wscat). +## Commands cheat sheet -| Command | Description | -|------------------------------------------|------------------------------------------| -| `cmake -S backend -B backend/build` | Generate build files (from project root) | -| `cd backend && cmake -S . -B build` | Generate build files (from backend dir) | -| `cd backend/build && make` | Compile the engine | -| `cd backend/build && make tests` | Build the test suite | -| `cd backend/build && ./tests` | Run the test suite | -| `cd backend/build && make check` | Build and run tests in one step | -| `cd backend/build && ./websocket_server` | Start the backend WebSocket server | +### Backend + +| Command | Description | +|---------|-------------| +| `cmake -S backend -B backend/build` | Configure from repo root | +| `cmake --build backend/build` | Build server | +| `cmake --build backend/build --target tests` | Build tests | +| `backend/build/tests` | Run tests | +| `backend/build/websocket_server` | Run WSS server (cwd with access to `certs/`) | ### Frontend -| Command | Description | -|-------------------|------------------------------------------| -| `npm run dev` | Start development server with hot reload | -| `npm run build` | Create production build in `dist/` | -| `npm run lint` | Check code for style issues | -| `npm run preview` | Preview the production build locally | +| Command | Description | +|---------|-------------| +| `npm run dev` | Dev server with HMR | +| `npm run build` | Production build | +| `npm run lint` | ESLint | +| `npm run preview` | Preview production build | ### Simulator -| Command | Description | -|---------------|---------------------------------| -| `npm run dev` | Start the market data simulator | +| Command | Description | +|---------|-------------| +| `npm run dev` | Start mock WS server on port 7000 | -## Development Workflow +## Troubleshooting -1. Start the backend first: `cd backend/build && ./websocket_server` -2. Start the simulator: `cd simulator && npm run dev` -3. Start the frontend: `cd frontend && npm run dev` -4. Open the frontend URL in your browser +**CMake cannot find OpenSSL** +Install `libssl-dev` (Debian/Ubuntu) or `openssl` (Homebrew). -## Troubleshooting +**Submodule checkout is empty** +From the repo root: `git submodule update --init --recursive`. + +**Browser WebSocket fails with a certificate error** +Expected with the self-signed cert. Trust `backend/certs/cert.pem` or use +`websocat --insecure wss://localhost:7000` for CLI testing. -**CMake can't find OpenSSL:** -Make sure libssl-dev (Debian/Ubuntu) or openssl (macOS) is installed. +**Port 7000 already in use** +Stop the other process. Backend and simulator both default to 7000. -**Submodule error:** -Run `git submodule update --init --recursive` from the project root. +## Further reading -**OpenSSL certificate warnings:** -The browser will show a warning for self-signed certificates. This is normal for local development. Click "Advanced" and proceed. +- [backend/README.md](backend/README.md) — build notes and WebSocket API +- [frontend/README.md](frontend/README.md) — client usage +- [backend/TODO.md](backend/TODO.md) — sketched binary trading protocol diff --git a/backend/README.md b/backend/README.md index 80a3fea..3eff164 100644 --- a/backend/README.md +++ b/backend/README.md @@ -62,7 +62,7 @@ All messages are JSON strings. Clients must authenticate before using any other #### Login Send: ```json -{"type": "login", "username": "test", "password": "test"} +{"type": "login", "username": "shashwat", "password": "shashwat"} ``` Response: ```json @@ -70,7 +70,7 @@ Response: ``` Other statuses: `"failed"`, `"already_logged_in"` -Default credentials: `test/test` +Default credentials: `shashwat`/`shashwat`, `nero`/`nero` #### Subscribe to Market Data Send: @@ -126,7 +126,7 @@ websocat -v --insecure wss://localhost:7000 Then type messages directly: ```json -{"type": "login", "username": "test", "password": "test"} +{"type": "login", "username": "shashwat", "password": "shashwat"} {"type": "subscribe", "channel": "market/snapshot"} ```