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")