How can I make SSE with Python (Django)?

后端 未结 2 1890
情话喂你
情话喂你 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条回答
  •  猫巷女王i
    2020-12-17 22:22

    After reading this, I think I understood the whole thing (please comment if I’m wrong).



    Django does NOT natively support keep-alive connections. 
This means, when the client gets the message from the server, the connection is immediately closed after (like any classic HTTP request/response cycle).

    
What’s different with text/event-stream request, is that the client automatically tries to reconnect to the server every second (the length can be changed with a retry parameter).



    Unfortunately, it seems using SSE in that case has no interest since it has the same con’s that polling (ie. a request/response cycle occurs each X seconds).

    

As expected and mentioned in other answers, I would need django-channels to create a persistent connection that prevent HTTP request/response overheads and ensure the message is sent immediately.


提交回复
热议问题