API Reference
sse_stream
django_flosse.decorators.sse_stream(_view_func: Optional[Callable] = None, *, retry: Optional[int] = None) -> Callable
Decorator that turns a generator view into an SSE endpoint.
Supports both sync and async generator views — detected automatically::
# sync
@sse_stream
def feed(request):
yield ("update", {"value": 1})
# async — requires ASGI server (Uvicorn, Daphne)
@sse_stream
async def feed(request):
async for item in my_async_source():
yield ("update", {"value": item})
Can be used with or without parentheses::
@sse_stream
@sse_stream()
@sse_stream(retry=3000)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry
|
Optional[int]
|
If given, sends a |
None
|
Yield styles
str→ plain data event(event_name, data)→ named event(event_name, data, id)→ named event with IDdictwith"data"key → mapped to SSEEvent fieldsdictwithout"data"→ whole dict serialised as JSON- :class:
~django_flosse.events.SSEEvent→ used directly
Heartbeats
django-flosse does not manage heartbeats automatically. Emit a keep-alive ping yourself if behind a proxy::
@sse_stream
async def live_feed(request):
while True:
data = await get_new_data()
if data:
yield ("update", data)
else:
yield SSEEvent(data="", event="ping")
await asyncio.sleep(5)
Source code in django_flosse/decorators.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
SSEEvent
django_flosse.events.SSEEvent
dataclass
Represents a single Server-Sent Event.
Attributes:
data: The payload. Dicts/lists are serialised to JSON automatically.
event: Optional named event type (maps to the event: field).
id: Optional event ID for client reconnection (maps to id:).
retry: Optional reconnection delay in milliseconds (maps to retry:).
Examples::
SSEEvent(data="hello")
SSEEvent(data={"count": 1}, event="update")
SSEEvent(data="done", event="finish", id="42")
Source code in django_flosse/events.py
encode() -> str
Serialise to the SSE wire format (ends with a blank line).
Source code in django_flosse/events.py
to_sse
django_flosse.formatters.to_sse(raw: Any) -> str
Convert any value yielded by a view into an SSE-formatted string.
Supported yield styles
str→ treated as plain data(event, data)→ named event with datadict→ passed as keyword args to SSEEvent; if no"data"key, the whole dict is the dataSSEEvent→ encoded as-is
Anything else is coerced to str and used as data.