API Reference
sse_stream
django_flosse.decorators.sse_stream(_view_func: Optional[Callable] = None, *, retry: Optional[int] = None, permission_classes: Sequence[Type[BaseSSEPermission]] = ()) -> 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, permission_classes=[IsAuthenticated])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry
|
Optional[int]
|
If given, sends a |
None
|
permission_classes
|
Sequence[Type[BaseSSEPermission]]
|
Sequence of :class: |
()
|
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
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
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.
Source code in django_flosse/formatters.py
Permissions
django_flosse.permissions.BaseSSEPermission
Minimal permission interface for @sse_stream.
Subclass this and override has_permission to add your own auth logic.
Returning False causes the decorator to respond with HTTP 403.
Example::
class LoginRequired(BaseSSEPermission):
def has_permission(self, request: HttpRequest) -> bool:
return request.user.is_authenticated
Source code in django_flosse/permissions.py
django_flosse.permissions.AllowAny
django_flosse.permissions.IsAuthenticated
Bases: BaseSSEPermission
Allow access only to authenticated users (session / token auth).
Source code in django_flosse/permissions.py
django_flosse.permissions.IsAdminUser
Bases: BaseSSEPermission
Allow access only to staff / superusers.