we're hiring! - disqus.com/jobs
Periodical requests to the server
So why did we "fix" it?
Coz it was broken.
var interval = setInterval(function () {
var cache = DISQUS.cache.realtime,
url = DISQUS.jsonData.urls.realtime;
url += '?timestamp=' + cache.last_checked +
'&thread_id=' + DISQUS.jsonData.thread.id +
'&f=' + DISQUS.jsonData.forum.url + '&';
if (!cache.ongoing_request && DISQUS.jsonData.realtime_enabled) {
if (cache.prev_script && cache.prev_script.parentNode) {
DISQUS.nodes.remove(cache.prev_script);
}
cache.ongoing_request = true;
cache.prev_script = DISQUS.request.get(url, undefined, true);
}
}, DISQUS.jsonData.context.realtime_speed);
var interval = setInterval(function () {
// Do nothing if the server didn't push anything new.
if (xhr.responseText && len !== xhr.responseText.length) {
/* ...try parsing responseText... */
/* ...trigger events etc... */
len = xhr.responseText.length;
}
if (xhr.readyState == 4) { // detect close
xhr.abort();
clearInterval(interval);
return void self.poll();
}
}, frequency);
brought to you by HTML5 and XHR2
onProgress: function () {
var resp = this.xhr.responseText;
var advance = 0;
var rows;
// Sanity check
if (!resp || this.marker >= resp.length)
return;
/* ...try parsing responseText... */
/* ...trigger events etc... */
if (advance > 0)
this.marker += (advance - 1); // -1 since no new line at the end
}
run: function () {
var socket = this.socket = new WebSocket(this.url);
socket.onmessage = this.onmessage.bind(this);
socket.onclose = this.close.bind(close);
},
onmessage: function (evt) {
var msg = JSON.parse(evt.data);
this.handleMessage(msg);
}
!!window.WebSocket
// Hi, I'm Safari 5.1, the smartest browser in the world who implemented
// an old WebSocket draft without a vendor prefix, just for fun!
window.WebSocket && WebSocket.CLOSING === 2,
onClose: function (evt) {
if (!evt.wasClean)
return; // means error, which is handled by onError
DISQUS.log('RT: [Socket] Connection closed. Restarting...');
this.trigger('close', this);
this.run();
}
onError: function () {
this.trigger('error');
if (this.interval <= BACKOFF_LIMIT)
this.interval *= EXP_BASE;
DISQUS.logError('RT: Connection error, backing off...');
_.delay(this._boundRun, this.interval * 1000);
}
onOpen: function () {
this.handshakeSuccess = true;
},
onError: function () {
if (!this.handshakeSuccess) {
this.trigger('fail');
return;
} /* ... */
},
initialize: function (threadId, handlers, context) {
/* ... */
if (ws) { // if browser supports WebSockets
pipe.on('fail', function () {
this._wsSupported = false;
/* reinit */
}, this);
}
/* ... */
}
Bleh...
id: 0ab2a49455664f0c9bca9a057b35af8a
event: Vote
data: { /* Some JSON data in a single line */ }
var handleGeoEvent = function(e) {
var geo = JSON.parse(e.data).message_body.geo;
addGeoPoint(geo.latitude, geo.longitude);
};
var ev = new EventSource("http://realtime.services.disqus.com/api/raw/orbital");
ev.addEventListener("Post", handleGeoEvent);
ev.addEventListener("Vote", handleGeoEvent);
ev.addEventListener("ThreadVote", handleGeoEvent);
* Already solved by WebSockets
Polyfills + if (window.EventSource)
If on different origin:
try {
var ev = window.EventSource && new EventSource(url);
catch (err) { // different origin will trigger an error immediately
// fallback
};
Or a static check that's built into Modernizr?
if (Modernizr.EventSourceWithCORS)
Use Nginx PushStream with EventSource, especially if you just need to update the client!
Psst... more about Nginx PushStream here: Making Disqus Realtime
Lego Star Wars Images by Mike Stimpson
come, join us! - disqus.com/jobs