Packet Loss Simulation
The RPG-JS Vite plugin includes a built-in packet loss simulation feature that allows you to test how your game behaves under poor network conditions.Overview
This feature simulates real-world network conditions by randomly preventing the server from processing certain messages received from clients, simulating what happens when the server doesn’t receive certain packets due to network issues. This is useful for:- Testing server-side message handling under poor network conditions
- Validating game state synchronization when messages are lost
- Ensuring your server handles missing client messages gracefully
- Debugging network-related issues in multiplayer games
Configuration
Environment Variables
You can configure packet loss simulation using environment variables:Programmatic Configuration
You can also configure packet loss simulation programmatically:Usage Examples
Basic Setup
Testing Different Network Conditions
Console Output
When packet loss simulation is enabled, you’ll see colored logs in your terminal:- Green:
[PACKET RECEIVED]- Message will be processed by server - Red:
[PACKET LOSS]- Message dropped (server won’t process it) - Yellow:
[PACKET DATA]- Content of the dropped message - Purple:
[PACKET LOSS SIMULATION]- Configuration changes - Cyan:
[NETWORK SIMULATION]- Startup status
Best Practices
- Use realistic rates: 5-15% packet loss is common in poor network conditions
- Test incrementally: Start with low rates and gradually increase
- Use filters strategically: Target specific message types (like ‘sync’ or ‘playerMove’) to test critical game systems
- Monitor server behavior: Ensure your server handles missing messages gracefully
- Test reconnection: Verify that clients can reconnect and sync state properly
- Disable in production: Always disable packet loss simulation in production builds
Packet Filtering
You can use theRPGJS_PACKET_LOSS_FILTER environment variable or the filter parameter in configurePacketLoss() to only simulate packet loss for specific types of messages. This is useful for:
- Testing only synchronization messages:
filter: 'sync' - Testing only player movement:
filter: 'playerMove' - Testing only battle updates:
filter: 'battle'
Implementation Details
The packet loss simulation works by:- Intercepting all incoming WebSocket messages in the
ws.on("message")handler - Generating a random number between 0 and 1
- Comparing against the configured loss rate
- Either processing the message normally or dropping it with a log message
Troubleshooting
Packet Loss Not Working
- Verify environment variables are set correctly
- Check that
RPGJS_ENABLE_PACKET_LOSS=true - Ensure
RPGJS_PACKET_LOSS_RATEis greater than 0 - Look for the startup log message confirming configuration
Too Much Packet Loss
- Reduce the
RPGJS_PACKET_LOSS_RATEvalue - Use values between 0.05 (5%) and 0.25 (25%) for realistic testing
No Logs Appearing
- Ensure your client is sending messages to the server
- Check that WebSocket connections are established
- Verify console output is not being filtered