Home / Function/ websocket() — fastapi Function Reference

websocket() — fastapi Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  7c16e84d_2ff5_4884_612a_8f36a61c9299["websocket()"]
  587454b3_6db6_011f_9fa4_3e4e2e09b72d["FastAPI"]
  7c16e84d_2ff5_4884_612a_8f36a61c9299 -->|defined in| 587454b3_6db6_011f_9fa4_3e4e2e09b72d
  ba33b279_1103_0775_a008_211bf9ab87de["add_api_websocket_route()"]
  7c16e84d_2ff5_4884_612a_8f36a61c9299 -->|calls| ba33b279_1103_0775_a008_211bf9ab87de
  b52314f4_2210_73b7_9368_6287f2dc93a0["websocket()"]
  7c16e84d_2ff5_4884_612a_8f36a61c9299 -->|calls| b52314f4_2210_73b7_9368_6287f2dc93a0
  style 7c16e84d_2ff5_4884_612a_8f36a61c9299 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/applications.py lines 1274–1337

    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[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**

        ```python
        from fastapi import FastAPI, WebSocket

        app = FastAPI()

        @app.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}")
        ```
        """

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

        return decorator

Domain

Subdomains

Frequently Asked Questions

What does websocket() do?
websocket() is a function in the fastapi codebase, defined in fastapi/applications.py.
Where is websocket() defined?
websocket() is defined in fastapi/applications.py at line 1274.
What does websocket() call?
websocket() calls 2 function(s): add_api_websocket_route, websocket.

Analyze Your Own Codebase

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

Try Supermodel Free