A secure WebSocket server and client implementation in Node.js that demonstrates real-time bidirectional communication. The application includes features like automatic reconnection, proper error handling, and connection management.
-
Secure WebSocket Server
- Origin validation for connection security
- Connection limit management to prevent DoS attacks
- Proper error handling and logging
- Support for both UTF-8 and binary messages
- Automatic ping/pong for connection health monitoring
-
Robust WebSocket Client
- Automatic reconnection on connection loss
- Graceful error handling
- Resource cleanup on disconnection
- Configurable message intervals
- Node.js (v14 or higher)
- pnpm (or npm)
- Clone the repository
- Install dependencies:
pnpm install
The application can be configured using environment variables. Create a .env
file in the root directory with the following options:
PORT=8080
ALLOWED_ORIGINS=http://localhost:3000,ws://localhost:8080
MAX_CONNECTIONS=100
WS_URL=ws://localhost:8080/
RECONNECT_INTERVAL=5000
MESSAGE_INTERVAL=1000
-
Start the WebSocket server:
pnpm start # or for development mode with auto-reload: pnpm run dev
-
In another terminal, start the client:
pnpm run start:client # or for development mode with auto-reload: pnpm run dev:client
- The server listens for WebSocket connections on the configured port
- When a client connects, the server validates the connection origin
- Upon successful connection:
- The client starts sending random numbers to the server every second
- The server echoes back each received message
- The server maintains a ping/pong mechanism to keep connections alive
- If the connection drops, the client automatically attempts to reconnect
The application implements comprehensive error handling:
- Connection validation errors
- Message processing errors
- Connection cleanup errors
- Automatic reconnection on failures
- Resource cleanup on termination
The project uses nodemon for development, which automatically restarts the application when files change. Use the dev
scripts for development:
pnpm run dev # For server
pnpm run dev:client # For client
ISC License