Home / Function/ websocket() — fastapi Function Reference

websocket() — fastapi Function Reference

Architecture documentation for the websocket() function in routing.py from the fastapi codebase.

Function python FastAPI Routing calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  b52314f4_2210_73b7_9368_6287f2dc93a0["websocket()"]
  ecadd3bc_0c58_b4e5_06d8_57da79199adc["APIRouter"]
  b52314f4_2210_73b7_9368_6287f2dc93a0 -->|defined in| ecadd3bc_0c58_b4e5_06d8_57da79199adc
  7c16e84d_2ff5_4884_612a_8f36a61c9299["websocket()"]
  7c16e84d_2ff5_4884_612a_8f36a61c9299 -->|calls| b52314f4_2210_73b7_9368_6287f2dc93a0
  50d27352_5915_310d_fe1d_f8784a990ec3["add_api_websocket_route()"]
  b52314f4_2210_73b7_9368_6287f2dc93a0 -->|calls| 50d27352_5915_310d_fe1d_f8784a990ec3
  c1d02f65_9a84_123a_bdd4_304e5732f35a["include_router()"]
  b52314f4_2210_73b7_9368_6287f2dc93a0 -->|calls| c1d02f65_9a84_123a_bdd4_304e5732f35a
  style b52314f4_2210_73b7_9368_6287f2dc93a0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/routing.py lines 1185–1250

    def websocket(
        self,
        path: Annotated[
            str,
            Doc(
                """
                WebSocket path.
                """
            ),
        ],
        name: Annotated[
            Optional[str],
            Doc(
                """
                A name for the WebSocket. Only used internally.
                """
            ),
        ] = None,
        *,
        dependencies: Annotated[
            Optional[Sequence[params.Depends]],
            Doc(
                """
                A list of dependencies (using `Depends()`) to be used for this
                WebSocket.

                Read more about it in the
                [FastAPI docs for WebSockets](https://fastapi.tiangolo.com/advanced/websockets/).
                """
            ),
        ] = None,
    ) -> Callable[[DecoratedCallable], DecoratedCallable]:
        """
        Decorate a WebSocket function.

        Read more about it in the
        [FastAPI docs for WebSockets](https://fastapi.tiangolo.com/advanced/websockets/).

        **Example**

        ## Example

        ```python
        from fastapi import APIRouter, FastAPI, WebSocket

        app = FastAPI()
        router = APIRouter()

        @router.websocket("/ws")
        async def websocket_endpoint(websocket: WebSocket):
            await websocket.accept()
            while True:
                data = await websocket.receive_text()
                await websocket.send_text(f"Message text was: {data}")

        app.include_router(router)
        ```
        """

        def decorator(func: DecoratedCallable) -> DecoratedCallable:
            self.add_api_websocket_route(
                path, func, name=name, dependencies=dependencies
            )
            return func

        return decorator

Domain

Subdomains

Defined In

Called By

Frequently Asked Questions

What does websocket() do?
websocket() is a function in the fastapi codebase, defined in fastapi/routing.py.
Where is websocket() defined?
websocket() is defined in fastapi/routing.py at line 1185.
What does websocket() call?
websocket() calls 2 function(s): add_api_websocket_route, include_router.
What calls websocket()?
websocket() is called by 1 function(s): websocket.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free