i still new using SSE and i have a question about SSE in Django version 3.2.5, i am using StreamingHttpResponse to send SSE response to EventSource client and it does work fine, my question that
why it takes to long to open the connection between backend and EventSource?
why it sends only 167 responses/32 seconds ?
i tried to open the code of StreamingHttpResponse but i didn't find anything related to the number of response
here in the code
def sse_movies(request):
def event_stream():
while True:
sleep(.2)
yield f"data: {datetime.time(datetime.now())}\n\n"
return StreamingHttpResponse(event_stream(), content_type='text/event-stream')
i am using sleep() to wait only 200/milliseconds for each iteration. but whenever send the EventSource it waits almost 32/seconds to initiate the connection with the back-end, and after it sends 167 requests then waits 2 seconds then sends another 167 requests once more and after the second 167 is sent it waits another 32 seconds
here is the code of EventSource client
let url = '/test/' +'sse/movies/'
let sse_client = new EventSource(url)
let movies = document.querySelector('#data-movies')
let movies_list = document.querySelector('#messages')
sse_client.onopen = function(message_event) {
console.log('opened')
}
console.log(sse_client)
sse_client.onmessage = (message_event) => {
console.log(message_event.data)
console.log(sse_client.readyState)
}
NOTE: when i remove white: True EventSource doesn't wait and sends requests as much as possible
maybe i misunderstand something here, but i hope the somebody could help me
i could figure out the issue.
it was n't in the code itself, but it was related with the buffer size of the webserver
so when i edited my code to be as below it worked fine:
def sse_movies(request):
def event_stream():
body = ''.join([letter * 6000 for letter in 'A'])
body_len = len(body)
print(body_len)
while True:
sleep(2)
yield f"data: {body}\n\n"
return StreamingHttpResponse(event_stream(), content_type='text/event-stream')
as you see above the minimum size for buffer is 6000/Characters i don't not how much that could be in bytes), but yeah it worked Alhumdulliah)
i really don't know that much about buffer/buffer-size/.. but i thought that it could be the issue