This project provides a WebSocket relay server that allows clients to connect to specific channels and relay messages between them. It includes a real-time analytics dashboard with authentication to monitor connections, messages, and channel activity.
-
WebSocket Relay Functionality
- Channel-based message relaying
- Automatic channel creation and cleanup
- Multiple clients can connect to the same channel
- Messages from one client are relayed to all other clients in the same channel
-
Analytics Dashboard
- Password-protected admin interface
- Real-time statistics and metrics
- Monitor active connections and channels
- Track message relay activity
- View client connection details
-
Security
- Simple password authentication for dashboard
- Session management
- Configurable admin password
- Node.js (v14 or higher recommended)
- npm (v6 or higher)
- Clone this repository:
git clone https://github.com/uditrajput03/ws-relay.git
cd ws-relay- Install dependencies:
npm install- Configure the server (optional):
# Set custom port and admin password as environment variables
export PORT=9000
export ADMIN_PASSWORD=your-secure-password- Start the server:
npm startThe server will start on the configured port (default: 8080).
You can use websocat to connect to the relay server:
websocat ws://your-server:8080/my-channel -websocat ws://your-server:8080/my-channel -Messages typed in one terminal will be relayed to the other terminal, and vice versa.
To create your own WebSocket clients that connect to the relay:
const socket = new WebSocket('ws://your-server:8080/my-channel');
socket.onopen = () => {
console.log('Connected to relay server');
};
socket.onmessage = (event) => {
console.log('Received message:', event.data);
};
// Send a message
socket.send('Hello from my client!');- Navigate to
http://your-server:8080/dashboardin your browser - Enter the admin password (default:
admin123) - View real-time statistics and monitor connections
- Summary Cards: Quick overview of total connections, active connections, channels, and messages
- Channel Metrics: View connections and message counts for each channel
- Active Clients: List of all currently connected clients with details
- Recent Activity: Log of recent connection and disconnection events
| Environment Variable | Description | Default Value |
|---|---|---|
| PORT | Server port number | 8080 |
| ADMIN_PASSWORD | Dashboard login password | admin123 |
- Change the default password: Always change the default admin password before deploying to production
- Use HTTPS in production: For production environments, set up HTTPS to secure connections
- Set secure cookies: Update the session configuration with
cookie: { secure: true }when using HTTPS
For production deployment, consider:
- Using a process manager like PM2:
npm install -g pm2
pm2 start server.js --name "websocket-relay"- Setting up a reverse proxy (Nginx/Apache) to handle HTTPS and load balancing
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.
Created by uditrajput03 | Last updated: 2025-07-11