Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I not getting a live response to Comet server stream events?
    primarykey
    data
    text
    <p>I have a CGI script that streams events:</p> <pre><code>#!/usr/bin/python ... print 'Content-Type: text/event-stream\n' while (True): delay() print 'event: item' print 'data: ' + get_random_event() print '' </code></pre> <p>From the command line, it sends random events at random intervals (every few seconds).</p> <p>However, I have the following JavaScript:</p> <pre><code>var source = new EventSource('feed.cgi'); source.addEventListener('message', function(current_event) { alert(current_event.data); }, false); </code></pre> <p>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.</p> <p>Do you have suggestions for how I can get these?</p> <p>--EDIT--</p> <p>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.</p> <p>Adding a file flush after an increment of behaviour doesn't seem to alter the script's reception.</p> <pre><code>#!/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 = '&lt;div class="activity"&gt;&lt;span class="name"&gt;' result += random.choice(names) result += '&lt;/span&gt; ' result += random.choice(notifications) result += '&lt;/div&gt;' return result print 'Content-Type: text/event-stream\n' while (True): print 'event: item' print 'data: ' + get_random_event() print '' sys.stderr.flush() delay() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload