Without having to reload websites!
Example: Realtime chat
function ajaxCall() {
$.get('/newmessages', function onComplete(response) {
addNewMessagesToChat(response);
// polling
setTimeout(ajaxCall, 1000);
}, 'json');
}
function cometCall() {
// get new messages from server
$.get('/newmessages', function (response) {
// instantly make new call
cometCall();
addNewMessagesToChat(response);
}, 'json');
};
var connection = new WebSocket(
'ws://socket.host.tld/chat'
);
connection.onmessage = function (message) {
addNewMessagesToChat(message.data);
};
$('#sendbutton').click(function () {
var text = $('#chatInput').val();
connection.send("message: " + text);
});
What about graceful degradation?
var socket = new io.Socket(hostname, {
port: port
});
socket.connect();
socket.on('message', function (message) {
addNewMessageToChat(message);
});
[..] a folk definition of insanity is to do the same thing over and over again and to expect the results to be different. By this definition, we in fact require that programmers of multithreaded systems be insane.
-- The Problem with Threads, Edward A. Lee, UC Berkeley, 2006
The better solutions to this problem are lightweight and provide only high concurrency