by Severin Ibarluzea and Joey Lee
// Constantly query web server for updates
setInterval(function(){
$.ajax({ url: '/mypage', success: function(data){
// Do stuff with message
} });
}, 100);
function load(){
$.ajax({ url: '/mypage', success: function(){
// do something with the data
}, complete: load, timeout: 20000 });
}
Polling is slow
Simulating sockets from scratch is tricky
Easy configuration
Easy to use server
npm install socket.io
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
// Do stuff with data
});
</script>
Client
Server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function (socket) {
socket.emit('news', "Hello World!");
});
Let's talk about the real problem
jk, but WebSockets are still great
http://socketo.me/