A WebSocket bridge server that connects the DefragLive Twitch bot running on Windows with the Twitch extension frontend, enabling real-time communication, game settings control, and translation services.
This server acts as a bridge between:
- DefragLive Bot (Windows) - The main bot controlling the Quake 3 Defrag game
- Twitch Extension (Frontend) - The user interface viewers interact with on Twitch
- Translation API - Google Translate integration for chat messages
- Real-time message broadcasting between bot and extension
- Game settings synchronization and control
- Translation services relay (handled by extension)
- Console message history persistence
- Server state management
- AFK detection and control
- Python 3.7+
- Docker (recommended)
- WebSocket connections from DefragLive bot and Twitch extension
- Clone this repository:
git clone <repository-url>
cd defrag-websocket-bridge- Build and run with Docker:
docker build -t defrag-bridge .
docker run -d --name defrag-bridge -p 8443:8443 -v $(pwd)/logs:/home/websocket_server/logs defrag-bridge- Install dependencies:
pip install -r requirements.txt- Run the server:
python server.py --host 0.0.0.0 --port 8443The server requires a Google Translate API key for translation services. Create a .env file:
cp .env.example .env
# Edit .env with your actual Google Translate API keyRequired Variables:
GOOGLE_TRANSLATE_API_KEY- Your Google Cloud Translation API key
Getting a Google Translate API Key:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Cloud Translation API
- Create credentials (API Key)
- Add the key to your
.envfile
/
├── server.py # Main WebSocket server
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── .env # Your environment variables (create this)
├── Dockerfile # Docker configuration
├── logs/ # Log files directory
├── console.json # Chat message history
├── serverstate.json # Current game server state
└── current_settings.json # Current game settings
The server automatically creates and manages these files:
console.json- Stores the last 100 chat messages for persistenceserverstate.json- Current game server state (players, map, etc.)current_settings.json- Current game settings (brightness, graphics, etc.)
These JSON files are automatically copied out of the Docker container via cron jobs to enable public HTTP access at:
https://tw.defrag.racing/console.json- Chat message historyhttps://tw.defrag.racing/serverstate.json- Current server state
This allows the Twitch extension and other services to access the data via standard HTTP requests without needing WebSocket connections.
Create cron jobs to copy the JSON files from the Docker container to your web server directory.
Add these lines to your crontab (crontab -e):
# Copy DefragLive data files every minute
* * * * * docker cp recursing_germain:/home/websocket_server/serverstate.json /var/www/html/
* * * * * docker cp recursing_germain:/home/websocket_server/console.json /var/www/html/Replace recursing_germain with your actual Docker container name, and adjust the destination path (/var/www/html/) to match your web server's document root.
If you experience issues with files being copied out of the Docker container, this is likely due to volume mounting or permission issues. To resolve:
- Ensure proper volume mounting:
docker run -d --name defrag-bridge \
-p 8443:8443 \
-v $(pwd)/logs:/home/websocket_server/logs \
-v $(pwd)/data:/home/websocket_server/data \
defrag-bridge- Set correct permissions:
mkdir -p logs data
chmod 755 logs data- Alternative: Use Docker volumes:
docker volume create defrag-logs
docker volume create defrag-data
docker run -d --name defrag-bridge \
-p 8443:8443 \
-v defrag-logs:/home/websocket_server/logs \
-v defrag-data:/home/websocket_server/data \
defrag-bridgepython server.py --host 0.0.0.0 --port 8443Or with Docker:
docker run -d --name defrag-bridge -p 8443:8443 defrag-bridgeThe server accepts WebSocket connections on ws://your-server:8443/
DefragLive Bot Connection:
- Bot identifies itself with
{"action": "identify_bot"} - Receives settings commands and sends game state updates
Twitch Extension Connection:
- Connects directly for real-time updates
- Sends user commands and receives game state/chat
{
"action": "identify_bot"
}
{
"action": "sync_settings",
"source": "defrag_bot",
"settings": {...}
}
{
"action": "serverstate",
"message": {...}
}{
"action": "ext_command",
"message": {
"content": {
"action": "settings_batch",
"settings": {...}
}
}
}
{
"action": "ext_command",
"message": {
"content": {
"action": "translate_message",
"cache_key": "...",
"text": "...",
"message_id": "..."
}
}
}The server supports these game settings:
triggers- Show trigger brushessky- Sky renderingclips- Show clip brushesslick- Highlight slick surfacesgamma- Display gamma (1.0-1.6)
brightness- Map brightness (1-5)picmip- Texture quality (0-5)fullbright- Fullbright mode (boolean)
drawgun- Draw weaponangles- Weapon angles displaylagometer- Network lagometersnaps- Snaps HUD elementcgaz- CGaz HUD elementspeedinfo- Speed info displayspeedorig- Original speed HUDinputs- Input display (WASD)obs- Overbounces indicator
nodraw- Player visibilitythirdperson- Third person viewminiview- Miniview windowgibs- Gibs after killblood- Blood after kill
The server includes a translation system with:
- Caching: Translations are cached to reduce API calls
- Rate Limiting: Prevents API quota exhaustion
- Cache Management: Automatically cleans old translations (max 500 entries)
Logs are written to:
- Console output (stdout)
logs/directory with timestamp-based filenames
Log format: MM/DD/YYYY HH:MM:SS message
-
Connection Refused:
- Check if the server is running
- Verify port 8443 is open
- Check firewall settings
-
Translation Not Working:
- Translation is handled by the extension
- Check extension configuration
- This server only relays translation requests
-
Settings Not Syncing:
- Ensure DefragLive bot is connected and identified
- Check WebSocket connection status
- Review server logs for command errors
-
File Persistence Issues:
- Check Docker volume mounts
- Verify file permissions
- Ensure sufficient disk space
Enable debug logging:
python server.py --host 0.0.0.0 --port 8443 --debug- Clone the repository
- Create virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create
.envfile with your configuration - Run server:
python server.py- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
[MIT License]
For issues and support:
- Open an issue on GitHub
- Check the logs directory for error details
- Verify your environment configuration