Forum Moderators: open

Message Too Old, No Replies

Difference between Ajax and Server-Sent Event Notifications

         

csdude55

12:01 am on Aug 28, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



After robzilla turned me on to LocalStorage, I came across Server-Sent Event Notifications, too:

[w3schools.com...]

I see that it's not compliant with IE at all, but I'm not sure how it's different from an Ajax command.

For example, this is a common usage of Ajax for me:

// Javascript / jQuery; I already use jQuery for other things so
// I'm using it here, too, but I used to do it with pure Javascript
setInterval("$('#example').ajax(path)", 15000);

// HTML
<div id="example"></div>


This would run a PHP script ever 15 seconds, and that PHP script checks a MySQL database for updates. Then it would update #example with the results of the PHP script.

How is this different from:

var source = new EventSource(path);
source.onmessage = function(event) {
$('#example').html(event.data);
};


Or, if I'm going to be compliant with IE:

if (typeof(EventSource) !== "undefined") {
var source = new EventSource(path + '?src=sse');
source.onmessage = function(event) {
$('#example').html(event.data);
};
}

else
setInterval("$('#example').ajax(path)", 15000);

// Then at the top of "path"
if ($_GET['src'] == 'sse') {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
}

// and at the bottom of "path"
if ($_GET['src'] == 'sse')
flush();


is SSE faster, or less work on the server... or is there some other advantage to using it over the Ajax script? Or am I completely misunderstanding the purpose of it altogether?

csdude55

2:04 am on Aug 31, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm still doing some digging here, and I'm getting mixed and confusing information.

As far as I can tell, an SSE opens a single HTTP request through Apache for every single person accessing that page. Then when there's an update, it sends an update to everyone on that page, through that single Apache process.

Here's where I'm confused, though. Let's say that I'm showing the user when they get a new Private Message... this will be the same PHP script for everyone: it reads a cookie for their username, and then does a MySQL query for that username.

But obviously, every person on the page shouldn't see the same result, it would be unique for each one of them. So is this a logical time to use SSE, or should it be an Ajax with setInterval()?

My instinct is that SSE makes more sense... I stay on a page for 10 minutes and setInterval() checks every 15 seconds, then there's a constant state of open-process-close-process; where, if I understand SSE correctly, there would just be the one process, and that's it. Right? Unless it sends the wrong info to everyone, which obviously wouldn't work for this application.

csdude55

2:20 am on Aug 31, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, and for future readers, there are a few other differences between the SSE script and the Ajax script...

1. SSE begins with echo "data: ";, because it sends a string back to event.data. I haven't tested whether I can use any word there (like "monkey: "; and event.monkey), but I suspect that I can.

2. The SSE script can't have any line breaks in it or the whole thing fails.

3. Here's what took me all dang day to figure out... at the very end, you have to end the data string with 2 line breaks. I don't know why, but just using 1 line break didn't work; no errors, just no output.

csdude55

7:48 am on Aug 31, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry to keep blowing this up, but...

I set up the above script and it seemed to be working fine. Then I went downstairs to watch TV and drink a beer (AMB Porter... if you haven't tried it, you really should). I came back up about 1 1/2 hours later, and had an error:

Failed to load resource: net::ERR_CONNECTION_RESET

That same error had happened before but I thought it was a fluke... a second time, though, and it's an actual problem.

I refreshed and kept an eye on the console, Munin, and top for an hour, but no errors came up and nothing seemed out of the ordinary on the server stats. I used a different computer to send myself a message and it updated immediately, and then I did the same thing about 30 minutes later and it still updated immediately, so I know that the script is still running. But I'm not sure how long it will take for the error to show up, or if it even happens every time.

The first error was at around 7pm, but this one happened sometime after 1am. 7pm would be near-peak, but 1am is about the slowest time of day for the server, so I can't imagine that other people were using things and typing up resources... and even if it was busy, nothing looks out of place on Munin. So I don't know if the problem is server related, ISP related, or what.

If it matters, I'm using Apache Apache/2.2.32, with KeepAlive disabled and Max Requests per Child at 10,000.

Any other suggestions on what the problem might be? Google found some other references to cookies and VPNs that didn't seem to apply, but I don't find a huge amount of info on SSE at all.