3

I am testing HTML5 server sent events to see if I can use it in my project. I tried to follow the tutorial on http://www.html5rocks.com/en/tutorials/eventsource/basics and http://www.w3schools.com/html/html5_serversentevents.asp, but could not get it to work.

Here's my html (sse.html) and python cgi script (sse.py):

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
   var source = new EventSource("/cgi-bin/sse.py");
   source.onmessage = function(event) {
       console.log(event.data);
   };
</script>
</head>
<body>
Testing SSE!
</div>
</body>
</html>

sse.py as follows:

import sys
import datetime
import time

sys.stdout.write('Content-type: text/event-stream\r\n\r\n')

while True:
   now = datetime.datetime.now()
   sys.stdout.write('data: %s\r\n\r\n' % now)
   sys.stdout.flush()
   time.sleep(5)

I put 'sse.html' into 'c:\code\sse', and 'sse.py' into 'c:\code\sse\cgi-bin'. Then, I ran 'python -m CGIHTTPServer' to bring up the development web server.

When I connected to 'http://localhost:8000/sse.html', I didn't see the console logging the data received from the server. Instead, I saw a message saying that "Resource interpreted as Script but transferred with MIME type text/plain: "chrome-extension://hgimnogjllphhhkhlmebbmlgjoejdpjl/bar.js"."

Does anyone know what would be the problem?

Thanks!

Share a link to this question
CC BY-SA 3.0
2

I modified 'sse.py' by trials and errors, and the code is working now. The new 'sse.py' is as follows:

import sys
import datetime
import time

sys.stdout.write('Content-type: text/event-stream \r\n\r\n')
now = datetime.datetime.now()
sys.stdout.write('data: %s \r\n' % now)
sys.stdout.write('retry: 1000\r\n\r\n')
sys.stdout.flush()

I am still trying to figure out how things work.

Share a link to this answer
CC BY-SA 3.0
5
  • I'm not sure 'retry' field solved your issue. But make sure that the response is encoded in UTF-8 and accordingly content-type header should be 'text/event-stream; charset=utf-8'. Mar 25 '15 at 14:30
  • 'retry' is for setting up the interval for client to visit the script again. If I take out the statement for setup the 'retry' interval, and add one more '\r\n' to the statement above. The code will still work, but the time interval will become 3 seconds. What I don't understand is that based on what I read, it seems like we only need to set the content type once, and the client will always wait for message with 'data:' prefix. But the modified code shows that it seems like we need to specify the content type for each message.
    – Victor Gau
    Mar 25 '15 at 15:04
  • I don't know Python so can't give you a clear answer but something is clearly wrong. As the author of real-time web application framework, Cettia, I've never used retry field in dealing with Server-Sent Events. Because you seem to print raw HTTP message unlike what you read (they are using framework not deal with HTTP message lines directly), I suspect you should follow Chunked Transfer encoding. Mar 25 '15 at 16:59
  • Then, the right plain message of HTTP response following event-stream format from Server-Sent Events spec will look like here See 'HTTP request/response for the client to receive data from the server' section. FYI, Vibe is a previous name of Cettia. Mar 25 '15 at 17:00
  • I think you need to look up nph scripts.
    – ijw
    Aug 15 '17 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.