Latency Simulation
RPG-JS provides built-in latency simulation capabilities to test how your game behaves under various network conditions. This feature allows you to simulate realistic network delays between the server and clients.Overview
Latency simulation adds configurable delays to outgoing messages from the server to clients, helping you:- Test game responsiveness under poor network conditions
- Validate client-side prediction and reconciliation systems
- Ensure smooth gameplay even with high latency
- Debug synchronization issues
Configuration
Environment Variables
You can configure latency simulation using environment variables:Programmatic Configuration
You can also configure latency simulation programmatically:Usage Examples
Basic Latency Simulation
Selective Latency
Combined with Packet Loss
You can use latency simulation together with packet loss simulation:Monitoring
The server logs will show latency simulation activity:Best Practices
1. Realistic Latency Values
Use realistic latency ranges based on your target network conditions:- Local/LAN: 1-10ms
- Good Internet: 20-100ms
- Poor Internet: 100-500ms
- Mobile/3G: 200-1000ms
2. Selective Application
Use filters to apply latency only to specific message types:3. Testing Different Scenarios
Test various latency scenarios:4. Client-Side Considerations
When latency simulation is enabled, ensure your client:- Implements proper client-side prediction
- Uses interpolation for smooth movement
- Handles message ordering correctly
- Provides visual feedback for network conditions
API Reference
PartyConnection.configureLatency()
Configures latency simulation settings. Parameters:enabled(boolean): Whether to enable latency simulationminMs(number): Minimum latency in millisecondsmaxMs(number): Maximum latency in millisecondsfilter(string, optional): Only apply latency to messages containing this string
PartyConnection.getLatencyStatus()
Returns current latency simulation configuration. Returns:Troubleshooting
Messages Not Being Delayed
- Check if latency simulation is enabled:
PartyConnection.getLatencyStatus() - Verify the filter string matches your message content
- Ensure
maxMsis greater than 0
Performance Issues
- High latency values may affect server performance
- Consider using filters to limit which messages are delayed
- Monitor server logs for excessive delay messages
Client Disconnections
- Very high latency may cause client timeouts
- Adjust client-side timeout settings accordingly
- Consider implementing heartbeat mechanisms