fanout / django-eventstream

Server-Sent Events for Django
MIT License
664 stars 88 forks source link

django-eventsteam vs django-grip #58

Closed t3ndai closed 4 years ago

t3ndai commented 4 years ago

had started experimenting with django-grip. what's the difference between the two libraries ?
also getting cors issues currently with django-grip

    <article hx-sse="connect localhost:8000/events/{{ item.id }}">
        {{  item.body | safe }} 
        {% if media %}
            <img src="{{ media.image }}" />
        {% endif %}
        <a href="{% url 'create-comment' item.id %}">reply</a>
    </article>

my view to subscribe to something

    client_details = {"id": request.user.pk, "item": item_id}
    set_hold_stream(request, f"items/{item_id}")
    return HttpResponse("connected", content_type="text/event-stream")
jkarneges commented 4 years ago

Hi @t3ndai,

django-eventstream is a library that provides SSE endpoints.

django-grip is a library for interfacing with GRIP proxies.

Note: django-eventstream has optional GRIP support, which it implements using the django-grip library.

It looks like you're trying to implement SSE. In that case, django-eventstream may be easier to use, especially if you want extra conveniences. But either way works. SSE is a simple protocol, and implementing something basic with django-grip is pretty easy.

That "connected" string is not valid SSE-formatting though. Try:

HttpResponse("event: message\ndata: connected\n\n", content_type="text/event-stream")

Regarding CORS issues, you'll want to respond with the appropriate Access-Control-* headers.

I hope that all makes sense.

t3ndai commented 4 years ago

@jkarneges thanks for your response. closing this out, though I have one question which is from django_eventstream import send_event i.e send_event is basically sugar for the http response I have on the original post right ? and if I use send_event then for the http response, I just need to return that it's a event stream ?

jkarneges commented 4 years ago

I suppose you could say the send_event function is like sugar for the HTTP response body, after the HTTP response code and headers have been sent.

But yeah, SSE requires basically two steps: 1) start a HTTP response with content type text/event-stream that stays open, 2) send SSE-encoded events in the response body.