Project

General

Profile

Need help for setting Server-Sent Events

Added by Joo Hong over 4 years ago

Hi,
I am using ubuntu 18.04 with lighttpd 1.4.45 and have been playing with Server-sent Event functionality, and unfortunately, not able to get it working, that is, no message from my cgi program. (I use FireFox 72.0.1)

lighttpd.conf:
server.modules = (
"mod_access",
"mod_alias",
"mod_cgi",
"mod_compress",
"mod_redirect",
)

server.document-root = "/var/www/html"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 80

index-file.names = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" )

  1. default listening port for IPv6 falls back to the IPv4 port
    1. Use ipv6 if available
      #include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
      include_shell "/usr/share/lighttpd/create-mime.assign.pl"
      include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

$HTTP["url"] =~ "^/cgi-bin/" {
alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin/" )
cgi.assign = ( ".cgi" => "",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php" )
}

cgi.assign = (
".cgi" => "",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php"

)

test_sse.html (in /var/www/html folder)

<!DOCTYPE html>
<html>
<body>

<h1>Getting server1 updates</h1>
<div id="result"></div>

<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("cgi-bin/test_sse.cgi");

source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "&lt;br&gt;";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
&lt;/script&gt;

</body>
</html>

test_sse.c (A simple cgi program written in C) (in /var/www/cgi-bin/test_sse.cgi)

#include "stdio.h"
#include "time.h"
#include <unistd.h>

int main(void) {
time_t now;
for (;;) {
printf("Cache-Control: no-cache\n\n");
printf("Content-Type: text/event-stream\n\n");
now = time(NULL);
printf("data: the server time is %ld\r\n",now );
sleep(2);
}
}

Could someone please help me what I am missing...

Thanks

Joo