Skip to content

Commit

Permalink
refactor(litestar): update litestar integration documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
andiserg committed Nov 2, 2024
1 parent b95d9a9 commit c8eadd6
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion docs/integrations/litestar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,41 @@ How to use
Websockets
**********************

Not supported yet
.. include:: _websockets.rst

In litestar, your view function is called once per event,
and there is no way to determine if it is an http or websocket handler.
Therefore, you should use the special ``inject_websocket`` decorator for websocket handlers.
Also decorator can be only used to retrieve SESSION-scoped objects.
To achieve REQUEST-scope you can enter in manually:

.. code-block:: python
@websocket_listener("/")
@inject_websocket
async def get_with_request(
a: FromDishka[A], # object with Scope.SESSION
container: FromDishka[AsyncContainer], # container for Scope.SESSION
data: dict[str, str]
) -> dict[str, str]:
# enter the nested scope, which is Scope.REQUEST
async with container() as request_container:
b = await request_container.get(B) # object with Scope.REQUEST
return {"key": "value"}
or with class-based handler:

.. code-block:: python
class Handler(WebsocketListener):
path = "/"
@inject_websocket
async def on_receive(
a: FromDishka[A], # object with Scope.SESSION
container: FromDishka[AsyncContainer], # container for Scope.SESSION
data: dict[str, str]
) -> dict[str, str]:
async with container() as request_container:
b = await request_container.get(B) # object with Scope.REQUEST
return {"key": "value"}

0 comments on commit c8eadd6

Please sign in to comment.