This project provides two different WebSocket-based setups for a Tic-Tac-Toe game:
- Basic WebSocket (ws package) – A simple WebSocket server using the
wsnpm package. - Socket.io with Express Backend – A WebSocket server using
socket.iointegrated with an Express server.
This setup uses the ws npm package to create a WebSocket server on port 8080. The client code is a simple index.html file that connects to the WebSocket server.
- WebSocket server runs on port
8080. - The client (
index.html) can be opened via a file explorer or a live server. - Two instances of the same page must be opened to play.
- The game starts when two players are connected.
- Moves and messages are communicated between the two players in real-time.
- No user-level identity check is implemented, so the same user can connect multiple times.
- Clone the project:
git clone <repository-url> cd tic-tac-ws
- Install dependencies:
npm install
- Start the WebSocket server:
npm run dev # Development mode npm run start # Production mode
- Open
index.htmlusing a live server or directly in the browser.
This setup uses an Express server with socket.io to handle WebSocket connections on the same port as the web server.
- WebSocket server runs alongside an Express backend.
- The game page is accessible via
http://localhost:3000/. - Client and server communicate over predefined
socket.ioevents. - Game win/draw status handling is implemented.
- User identity is maintained using browser cookies, preventing the same user from connecting multiple times as different players.
- Clone the project if not already done:
git clone <repository-url>
- Navigate to the Express-based setup:
cd tic-tac-ws/express-socketio - Install dependencies:
npm install
- Start the WebSocket + Express server:
npm run dev:socketio # Development mode npm run start:socketio # Production mode
- Open the game page in a browser:
http://localhost:3000/
- Ensure
Node.jsis installed before running the project. Node@18 and above. - For the basic WebSocket version, use a live server for better performance.
- Both setups require two players to start a game.
- The Express + Socket.io version maintains a browser-level identity check using cookies, preventing duplicate connections from the same user.
Happy Coding! 🎮