How can I make SSE with Python (Django)?

后端 未结 2 1895
情话喂你
情话喂你 2020-12-17 21:28

I have two different pages, one (A) that displays data taken from a model object, and one (B) that changes its fields. I would like that when the post data is sent from B to

2条回答
  •  没有蜡笔的小新
    2020-12-17 22:13

    This is working example from w3schools in Django:

    template

    
    
    
    
    

    Getting server updates

    views

    import datetime
    import time
    from django.http import StreamingHttpResponse
    
    def stream(request):
        def event_stream():
            while True:
                time.sleep(3)
                yield 'data: The server time is: %s\n\n' % datetime.datetime.now()
        return StreamingHttpResponse(event_stream(), content_type='text/event-stream')
    

    urls

    urlpatterns = [
        path('stream/', views.stream, name='stream')
    ]
    

    Update:

    If you want to manage your notifications you can create the model like:

    from django.db import models
    
    class Notification(models.Model):
        text = models.CharField(max_length=200)
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        sent = models.BooleanField(default=False)
    

    Then create the view that is looking for the first unsent notification and sends it:

    @login_required
    def stream(request):
        def event_stream():
            while True:
                time.sleep(3)
                notification = Notification.objects.filter(
                    sent=False, user=request.user
                ).first()
    
                text = ''
    
                if notification:
                    text = notification.text
                    notification.sent = True
                    notification.save()
    
                yield 'data: %s\n\n' % text
    
        return StreamingHttpResponse(event_stream(), content_type='text/event-stream')
    

    And the send_notification function that creates an entry in the Notification model (just call this function from anywhere in your code):

    def send_notification(user, text):
        Notification.objects.create(
            user=user, text=text
        )
    

    That's it, simple as that.

提交回复
热议问题