0

Server Sent Event Does Not Maintain Open Connection

I’ve been working with some example code that I found on the web to get some basic server sent events working on our server, but I’ve stumbled across some strange behavior when testing across servers. Below is the function being called in my controller:

public function hi() {
    header("Content-Type: text/event-stream\n\n");

    $counter = rand(1, 10);
    while (1) {
        // Every two seconds, send a "ping" event.

        echo "event: ping\n";
        $curDate = date(DATE_ISO8601);
        echo 'data: {"time": "' . $curDate . '"}';
        echo "\n\n";


        ob_flush();
        flush();
        sleep(2);
    }
}

My View:

<script>
var evtSource = new EventSource("picks/hi");

evtSource.onmessage = function(e) {
  console.log(e.data);
}

evtSource.onerror = function(e) {
  console.log("EventSource failed.");
};

evtSource.onopen = function(e) {
  console.log("Connection open");
};

evtSource.addEventListener("ping", function(e) {
  var newElement = document.createElement("li");

  var obj = JSON.parse(e.data);
  console.log(obj.time);
}, false);
</script>

Now here’s the bizarre part. This code works as it should on my hostgator account. It opens a connection, maintains it, and spits out the time every two seconds. If you view the console in Chrome, you will see the following output:

Connection open 
2014-04-02T15:10:24-0400 
{"time": "2014-04-02T15:10:26-0400"} 
{"time": "2014-04-02T15:10:28-0400"} 
{"time": "2014-04-02T15:10:30-0400"} 
{"time": "2014-04-02T15:10:32-0400"} 
{"time": "2014-04-02T15:10:34-0400"} 
{"time": "2014-04-02T15:10:36-0400"} 
{"time": "2014-04-02T15:10:38-0400"} 

Unfortunately, this same block of code does not work on our server through Amazon Web Services. When I use the console to compare what is going on between the two, I can see that the code on the hostgator server does indeed open and maintain a persistent connection. When I do the same on the AWS instance, I see that the GET request for data is stuck in a pending status. I believe this is an apache issue, but I cannot find any information. What do I need to do on the server side to enable server sent events to properly function?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Derek
  • 1
  • 1
  • By "AWS" do you mean you are running an EC2 server, and I assume it is Linux, running Apache and PHP? And you do the server admin yourself? Are the two servers running the same distro, Apache version and PHP version? Are you using a load balancer on the AWS side? (Sorry for so many questions, but it should "just work" :-) – Darren Cook Apr 03 '14 at 07:51
  • Hi there, we're getting the same issue with SSEs not working with AWS servers. Did you get a solution to your issue? – DuncanKinnear Jan 21 '19 at 21:00

2 Answers2

0

A few things to check...are your server and client side scripts both of the same origin? I know this can cause issues in some browser, but admittedly it's been a while since I played around with SSE's, so I don't know if this still holds true.

You may also want to add header('Cache-Control: no-cache'); under your content-type declaration just be certain.

You may have to contact AWS support to see what their thoughts are.

Also, and I know this sounds stupid, but completely clear all of your browser info after changing your code (cookies, cache, etc...).

kevindeleon
  • 1,914
  • 3
  • 18
  • 30
  • Yes, both the server and client scripts are of the same origin for each installation. I'm using codeigniter for MVC and both scripts are set up using the same pattern. I've also added the additional Cache-Control declaration but it doesn't seem to affect the results. AWS support has been a bit lacking so I thought I would try here, but I may just have to continue to pester them. – Derek Apr 02 '14 at 20:38
0

If your server is running behind nginx then you need to add

    proxy_buffering off;

to nginx settings. For apache, look into this link: Prevent output buffering with PHP and Apache

Community
  • 1
  • 1