APIWebSocketRoute
This class represents a WebSocket route that integrates with a dependency injection system to handle incoming connections. It manages path parsing, endpoint execution, and the resolution of required dependencies for the associated WebSocket application. The class also ensures that route metadata is correctly associated with the connection scope during request matching.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path | str | The URL path pattern used to match incoming WebSocket connection requests. |
| endpoint | Callable[..., Any] | The callable function or class used to handle the WebSocket communication logic. |
| name | str | The unique identifier for the route, used for URL generation; defaults to the endpoint function name. |
| dependencies | list | A list of dependency requirements that must be resolved before the endpoint is executed. |
| path_regex | Pattern | A compiled regular expression used to match the request path against this route. |
| path_format | str | A string representation of the path used for generating URLs with path parameters. |
| param_convertors | dict | A dictionary mapping path parameter names to their respective converter objects for type validation. |
| dependant | [Dependant](../dependencies/models/dependant.md?sid=fastapi_dependencies_models_dependant) | An object containing metadata about the endpoint's dependencies and parameters for validation and injection. |
| app | Callable | The final ASGI application wrapper that manages the WebSocket session and dependency injection lifecycle. |
Constructor
Signature
def APIWebSocketRoute(
path: str,
endpoint: Callable[..., Any],
name: str | None = None,
dependencies: Sequence[params.Depends]| None = None,
dependency_overrides_provider: Any | None = None
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| path | str | The URL path for the WebSocket route. |
| endpoint | Callable[..., Any] | The function to be called when the route is accessed. |
| name | `str | None` = None |
| dependencies | `Sequence[params.Depends] | None` = None |
| dependency_overrides_provider | `Any | None` = None |
Signature
def APIWebSocketRoute(
path: str,
endpoint: Callable[..., Any],
name: str | None = null,
dependencies: Sequence[params.Depends]| None = null,
dependency_overrides_provider: Any | None = null
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| path | str | The URL path pattern where the WebSocket connection will be accepted. |
| endpoint | Callable[..., Any] | The function or class to be executed when a client connects to this route. |
| name | `str | None` = null |
| dependencies | `Sequence[params.Depends] | None` = null |
| dependency_overrides_provider | `Any | None` = null |
Methods
matches()
@classmethod
def matches(
scope: Scope
) - > tuple[Match, Scope]
Determines if the incoming ASGI scope matches this route's path and updates the scope with route-specific metadata if a match is found.
Parameters
| Name | Type | Description |
|---|---|---|
| scope | Scope | The ASGI scope dictionary containing information about the incoming connection request. |
Returns
| Type | Description |
|---|---|
tuple[Match, Scope] | A tuple containing the match status and the potentially modified ASGI scope including the current route instance. |