Server Sent Events - SSE

Web page display of latest server package versions

We wanted a display on the website that would show the versions of all the major software packages that we had installed on the server. We would determine the packages we wanted to keep track of, and after that, the web display should automatically update on a regular basis.

Linux shell script

A Crontab task was set up to run a Linux shell script on a regular basis. The script does the following tasks:

  • Find versions of a pre-determined list of software packages, and extract these version numbers from varying amounts of ancilliary text
  • Create key value pairs of the package names and versions numbers and add some basic html formatting
  • Store these as one string in a simple log file. We could have used JSON here.

Server Events and Client Listeners

We created a server side file (in PHP) which would be the source of the events, and this would access data for two separate events:

  • The string generated by Crontab, which would show package versions
  • A time function which would show the server time
When the client acceses the server side file, the latter sends back a simple HTTP message including:
  • A header - Content-Type: text/event-stream
  • Two event blocks of code which specify the data for the two events ie. the string of versions, and the server time
The text/event-stream header when read by the browser, causes the latter to constantly reconnect to the server side file. The reconnection period can be controlled and here we have set this to be about 5 seconds.

In the client web pages we create an EventSource object using a JS script and this EventSource points to the server side file, and also creates listeners for the two events.

If no separate events had been specified in the server file, 'onmessage' could have been used instead of the listeners, but as we named two separate events we use addEventListener(eventname,...) with the EventSource in the js script.

Thes events appear below as a list of packages and versions, and a time.

The restriction of this method is that the two events are refreshed at the same the page reloads.

Automated Server Package Version Reporting

The installed versions are:

SSE inactive

Timer Update

The server time is:

SSE inactive

To Close the Connection