WebSockets
Video Course on Real-Time HTML5 with Node.jsnobody does WebSockets directly without a facade, but use frameworks e.g. socket.io or pusherajax is a roundtrip
there's an overhead for client initiating a request, the server a response and http packerts have extra 200 or more bytes
ajax is only when you need updates every 5 minutes, but not when 5 request per second (e.g. games)
in average an ajax request is about 500 - 800ms roundtrip in websocket, there's an inital socket opening, then in either direction the header is 8 bytes, we don't pay the penalty to open and close connection
the latency is 50 - 100 ms
but even 100ms can be too slow in fact
"real-time" is a misnomer : it's sub microsecond or nanosecond we mean "near real-time"
it's more resource intensive for the server: you need so many ports openbut in general it uses less bandwidth
the pattern is same for client or server. in generalsocket.on("eventname" function(data){
self.postMessage({error: "game_session_invalid"});
});
and
socket.emit("another_event_name", {game_session_id: bar});
client and server can listen to send events (
emit
) and send data (JS objects) along in addition the server can
broadcast
a message to all clients (it doesn't exist on the client)