From 65efff126be14d74e794ed76244b80942f530067 Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 8 Mar 2024 18:34:30 +0100 Subject: [PATCH] fix: sending empty data in sse in js client (#3176) fix sending empty data in sse --- litestar/response/sse.py | 2 +- test_apps/sse/__init__.py | 0 test_apps/sse/sse.html | 30 ++++++++++++++++++++++++++++++ test_apps/sse/sse_empty.py | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test_apps/sse/__init__.py create mode 100644 test_apps/sse/sse.html create mode 100644 test_apps/sse/sse_empty.py diff --git a/litestar/response/sse.py b/litestar/response/sse.py index 59576c5208..48a9192b98 100644 --- a/litestar/response/sse.py +++ b/litestar/response/sse.py @@ -91,7 +91,7 @@ async def _async_generator(self) -> AsyncGenerator[bytes, None]: @dataclass class ServerSentEventMessage: - data: str | int | bytes | None = None + data: str | int | bytes | None = "" event: str | None = None id: int | str | None = None retry: int | None = None diff --git a/test_apps/sse/__init__.py b/test_apps/sse/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test_apps/sse/sse.html b/test_apps/sse/sse.html new file mode 100644 index 0000000000..8998ccf93a --- /dev/null +++ b/test_apps/sse/sse.html @@ -0,0 +1,30 @@ + + + +

Server-Sent Events

+
+ + + diff --git a/test_apps/sse/sse_empty.py b/test_apps/sse/sse_empty.py new file mode 100644 index 0000000000..0e727f64fd --- /dev/null +++ b/test_apps/sse/sse_empty.py @@ -0,0 +1,23 @@ +from typing import AsyncIterator + +import uvicorn + +from litestar import Litestar, get +from litestar.config.cors import CORSConfig +from litestar.response import ServerSentEvent, ServerSentEventMessage +from litestar.types import SSEData + + +@get("/test_sse_empty") +async def handler() -> ServerSentEvent: + async def generate() -> AsyncIterator[SSEData]: + event = ServerSentEventMessage(event="empty") + yield event + + return ServerSentEvent(generate()) + + +app = Litestar(route_handlers=[handler], cors_config=CORSConfig(allow_origins=["*"])) + +if __name__ == "__main__": + uvicorn.run("sse_empty:app")