WebSocket Implementation
This document provides an overview of the WebSocket implementation in the Bedrock Server Manager, covering backend (server-side) components.
The backend WebSocket implementation is built using FastAPI and is divided into two main components: a WebSocket router and a connection manager.
WebSocket Router
The WebSocket router is defined in src/bedrock_server_manager/web/routers/websocket_router.py. It is responsible for the following:
Endpoint: Creates a WebSocket endpoint at
/ws.Authentication: Uses a dependency to authenticate the user before establishing a connection.
Message Handling: Listens for incoming JSON messages from the client. These messages are expected to have an
action(subscribeorunsubscribe) and atopic.Connection Management: Hands off the connection and subscription management to the
ConnectionManager.
Connection Manager
The ConnectionManager is a class defined in src/bedrock_server_manager/web/websocket_manager.py. It is the core of the backend WebSocket implementation and is responsible for the following:
Connection Tracking: Keeps track of all active WebSocket connections.
Topic-Based Subscriptions: Manages which clients are subscribed to which topics.
Message Broadcasting: Provides methods for sending messages to a single client, all clients subscribed to a specific topic, or all clients for a specific user.
Architecture
A client connects to the
/wsendpoint.The
websocket_routerauthenticates the user.The
websocket_routerpasses the connection to theConnectionManager.The client sends a
subscribemessage to a topic.The
websocket_routercalls theConnectionManagerto subscribe the client to the topic.When an event occurs on the server (e.g., the server status changes), the
ConnectionManageris used to broadcast a message to all clients subscribed to the relevant topic.
Available Topics
The following WebSocket topics are available for subscription:
Event Topics
Event topics broadcast a message when a specific event occurs on the server. The topic name is in the format event:{event_name}.
event:before_addon_importevent:after_addon_importevent:before_backupevent:after_backupevent:before_restoreevent:after_restoreevent:before_prune_backupsevent:after_prune_backupsevent:before_players_addevent:after_players_addevent:before_player_db_scanevent:after_player_db_scanevent:before_server_startevent:after_server_startevent:before_server_stopevent:after_server_stopevent:before_command_sendevent:after_command_sendevent:before_allowlist_changeevent:after_allowlist_changeevent:before_server_installevent:after_server_installevent:before_server_updateevent:after_server_updateevent:before_autostart_changeevent:after_autostart_changeevent:after_server_statuses_updatedevent:before_web_server_startevent:after_web_server_startevent:before_web_server_stopevent:after_web_server_stopevent:before_world_exportevent:after_world_exportevent:before_world_importevent:after_world_importevent:before_world_resetevent:after_world_reset
Resource Monitor Topics
Resource monitor topics broadcast resource usage information for a specific server. The topic name is in the format resource-monitor:{server_name}. Replace {server_name} with the name of the server you want to monitor.