Skip to content

Commit

Permalink
docs: mention how to use ServerSentEventMessage (#3170)
Browse files Browse the repository at this point in the history
  • Loading branch information
euri10 authored Mar 5, 2024
1 parent 9120d2f commit 368b22f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/usage/responses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,16 @@ which is used in for sending pings, and ``retry_duration``, which dictates the d
You can use different kinds of values for the iterator. It can be a callable returning a sync or async generator,
a generator itself, a sync or async iterator class, or an instance of a sync or async iterator class.

In your iterator function you can yield integers, strings or bytes, the message sent in that case will have ``message``
as the ``event_type`` if the ServerSentEvent has no ``event_type`` set, otherwise it will use the ``event_type``
specified, and the data will be the yielded value.

If you want to send a different event type, you can use a dictionary with the keys ``event_type`` and ``data`` or the :class:`ServerSentMessage <.response.ServerSentEventMessage>` class.

.. note::

You can further customize all the sse parameters, add comments, and set the retry duration by using the :class:`ServerSentEvent <.response.ServerSentEvent>` class directly or by using the :class:`ServerSentEventMessage <.response.ServerSentEventMessage>` or dictionaries with the appropriate keys.


Template Responses
------------------
Expand Down
3 changes: 2 additions & 1 deletion litestar/response/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .base import Response
from .file import File
from .redirect import Redirect
from .sse import ServerSentEvent
from .sse import ServerSentEvent, ServerSentEventMessage
from .streaming import Stream
from .template import Template

Expand All @@ -10,6 +10,7 @@
"Redirect",
"Response",
"ServerSentEvent",
"ServerSentEventMessage",
"Stream",
"Template",
)

0 comments on commit 368b22f

Please sign in to comment.