Skip to main content

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

AttributeTypeDescription
pathstrThe URL path pattern used to match incoming WebSocket connection requests.
endpointCallable[..., Any]The callable function or class used to handle the WebSocket communication logic.
namestrThe unique identifier for the route, used for URL generation; defaults to the endpoint function name.
dependencieslistA list of dependency requirements that must be resolved before the endpoint is executed.
path_regexPatternA compiled regular expression used to match the request path against this route.
path_formatstrA string representation of the path used for generating URLs with path parameters.
param_convertorsdictA 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.
appCallableThe 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

NameTypeDescription
pathstrThe URL path for the WebSocket route.
endpointCallable[..., Any]The function to be called when the route is accessed.
name`strNone` = None
dependencies`Sequence[params.Depends]None` = None
dependency_overrides_provider`AnyNone` = 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

NameTypeDescription
pathstrThe URL path pattern where the WebSocket connection will be accepted.
endpointCallable[..., Any]The function or class to be executed when a client connects to this route.
name`strNone` = null
dependencies`Sequence[params.Depends]None` = null
dependency_overrides_provider`AnyNone` = 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

NameTypeDescription
scopeScopeThe ASGI scope dictionary containing information about the incoming connection request.

Returns

TypeDescription
tuple[Match, Scope]A tuple containing the match status and the potentially modified ASGI scope including the current route instance.