Why am I not getting a live response to Comet server stream events?
Question: I have a CGI script that streams events:

    
    
    #!/usr/bin/python
    ...
    print 'Content-Type: text/event-stream\n'
    
    while (True):
        delay()
        print 'event: item'
        print 'data: ' + get_random_event()
        print ''
    

From the command line, it sends random events at random intervals (every few
seconds).

However, I have the following JavaScript:

    
    
    var source = new EventSource('feed.cgi');
    
    source.addEventListener('message', function(current_event)
        {
        alert(current_event.data);
        }, false);
    

This has never got me an alert. There are no errors in the Chrome JavaScript
console, but so far I have never gotten a discernible recognition that the
code listening to the server script has received an event.

Do you have suggestions for how I can get these?

\--EDIT--

I should probably give the first CGI script for completion. FireFox says
"Firefox can't establish a connection to the server at
http://[deleted]/feed.cgi." But I can execute a CGI script normally that's in
a sibling directory, and Apache's finest-grained Directory setting is still on
a directory that is parent to both and meant to allow scripts with a .cgi
extension to run as CGI. And the script is set to mode 0755.

Adding a file flush after an increment of behaviour doesn't seem to alter the
script's reception.

    
    
    #!/usr/bin/python
    
    import random
    import sys
    import time
    
    timescale = 5
    
    names = [
        'Adam',
        'Bob',
        'Carol',
        'David',
        'Eddie',
        'Frank',
        'George',
        'Harry',
        'Ike',
        'Jim',
        'Kenny',
        'Larry',
        'Mary',
        'Nancy',
        'Oliver',
        'Peter',
        'Quincy',
        'Roger',
        'Sam',
        'Thomas',
        'Uncle',
        'Vincent',
        'William',
        'Xavier',
        'Yogi',
        'Zachary',
        ]
    
    notifications = [
        'applied for a position at ACME.',
        'went to an interview at ACME.',
        'accepted an offer from ACME.',
        ]
    
    def delay():
        time.sleep((random.random() + random.random()) * timescale)
    
    def get_random_event():
        result = '<div class="activity"><span class="name">'
        result += random.choice(names)
        result += '</span> '
        result += random.choice(notifications)
        result += '</div>'
        return result
    
    print 'Content-Type: text/event-stream\n'
    
    while (True):
        print 'event: item'
        print 'data: ' + get_random_event()
        print ''
        sys.stderr.flush()
        delay()
    


Answer: Starting from our code and [W3chools Server Sent Event
documentation](http://www.w3schools.com/html/html5_serversentevents.asp) I
build an example which runs succesfully in chomium and firefox with these
modifications (with lighttpd):

  1. add a '\n' at the end of print
  2. flush the standard output insteadof the standard error

So the while should be:

    
    
    while (True):
        print 'event: item' + '\n'
        print 'data: ' + get_random_event() + '\n'
        print ''
        sys.stdout.flush()
        delay()
    

