Overview
Server messages are JSON objects sent from the MARL WebSocket server to your client application. These messages provide real-time updates about agents, simulation state changes, and system events. All messages follow a consistent format withtype and payload fields.
Message Format
Message Types
add_agent
Sent individually for each agent when the client requests initial agents or when new agents are dynamically added to the simulation. Direction: Server → Client (to requesting client) Payload:path_coordsisnullfor pedestrians (they don’t follow road networks)- Elevation data is included for realistic 3D positioning
velocitycan be used to calculate agent orientationattributesfield reserved for future customization
initial_population_complete
Sent after all initial agents have been generated and sent via individualadd_agent messages.
Direction: Server → Client (to requesting client)
Payload:
- Hide loading indicators
- Enable user interface controls
- Begin simulation updates
- Initialize performance monitoring
new_path
Sent in response to a successfulrequest_new_path message from the client.
Direction: Server → Client (to requesting client)
Payload:
- Only sent if path generation succeeds
path_coordsmay benullfor pedestrians- Includes current fleeing status (may have changed due to stimulus)
- Path includes elevation data for each waypoint
update_flee_status
Broadcast to all connected clients when one or more agents’ fleeing status changes due to a stimulus event. Direction: Server → All Clients (broadcast) Payload:- Fleeing Agents: Move faster, may ignore normal pathfinding
- Normal Agents: Resume regular behavior patterns
- Visual Indicators: Color changes, animations, effects
ping
Sent periodically by the server to maintain WebSocket connection and detect unresponsive clients. Direction: Server → Client Payload:null or empty object
- Sent every 30 seconds by default
- Used for connection keep-alive
- No response required from client
- Can be used to detect connection issues
error
Sent when the server encounters an error processing a client request. Direction: Server → Client (to requesting client) Payload:- Invalid coordinates in
request_initial_agents - Agent not found in
request_new_path - Simulation initialization failures
- Network or processing errors
Advanced Message Handling
Message Router
Agent State Management
Performance Monitoring
Error Recovery Patterns
Graceful Degradation
Best Practices
- Handle All Message Types: Implement handlers for all message types you might receive
- Validate Message Structure: Check for required fields before processing
- Error Recovery: Implement graceful degradation for connection issues
- Performance Monitoring: Track message frequency and processing time
- State Synchronization: Keep local state in sync with server updates
- Memory Management: Clean up agent references when no longer needed
Next Steps
- Review Client Messages to understand the full communication flow
- Explore the WebSocket Overview for integration patterns
- Check out Risk Analysis for enhanced simulation capabilities