Skip to main content

Django async Server Sent Events helpers

Project description

Django Async Server Sent Events Helpers

Helpers to make Server Sent Events in Django easier and async.

SSE Reference

See: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events

Install

pip install django-async-sse

Setup

Write an Event Stream

SseStreamView is a class-based view. Add an async stream method that uses yield to send events. Otherwise known as an async iterator.

Example SSE View:

import asyncio
from django_asse import SseStreamView, JsonEvent

class WeatherStream(SseStreamView):
  async def stream(self, request, lat, lng):
    last_update = None

    while 1:
      wp = await WeatherPoint.objects.filter(point=f"{lat},{lng}").afirst()

      if wp and wp.created != last_update:
        event = JsonEvent(event='weather', data=wp.weather_data['current'])
        last_update = wp.created
        # yield an event to send it
        yield event

      # always send a ping to keep connection from timing out
      # Note: needed for some deployments like Heroku
      yield JsonEvent(event='ping', data=None)
      await asyncio.sleep(30)

Connect View to a URL

urlpatterns = [
    ...
    re_path(r"weather/(-?\d{1,3}.\d{2}),(-?\d{1,3}.\d{2})/", WeatherStream.as_view()),
]

Run an ASGI server

pip install uvicorn[standard]
uvicorn myproject.asgi:application --loop uvloop

Included Helpers

  • Event: Event where data are strings.
  • JsonEvent: Event where data is encoded as JSON.
  • SseStreamView: Class-based view to generate an async streaming response. Inherit from this class and add an async stream method that is an async iterator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_async_sse-1.0.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

django_async_sse-1.0.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file django_async_sse-1.0.0.tar.gz.

File metadata

  • Download URL: django_async_sse-1.0.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.10.4 CPython/3.10.12

File hashes

Hashes for django_async_sse-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5fcc786ea76c6fbb34e3a88a8757c43e2cff528778e855c2d3085588994d7a01
MD5 fa60577caf9b40597fe712a1c5c4e4cf
BLAKE2b-256 c1abf0c5a6b26bc4aec676a7599619d44cf50c518b3439d04c0f8e0cb3250896

See more details on using hashes here.

File details

Details for the file django_async_sse-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_async_sse-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ccabba818a2495c547f3541f7b161b74a6e24126af2896030e7f779db30db29
MD5 cddbc80d70c9cbba74785d78f3662240
BLAKE2b-256 449e4294916eedfa373cad2eb32f91c26d733d92eea15d201d8de32a89a22645

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page