Building a Real-Time Web App with AJAX .post() – Step-by-Step Guide

ajaxjqueryreal timeweb-applications

Usually, real-time web-apps are built with websockets, right?

Well, let me be radical here – what if I used Ajax?

Okay, okay, I know it has its limitations. You can't build Agar.io, sending data packets up to fifty times a second, with latency in the milliseconds.

But what about an app that had real-time updates that were a bit less frequent. Maybe receiving notifications with in a few seconds, or a turn-based game?

Is it acceptable to use Ajax.post() every, say, few seconds to receive updates from a, say, PHP script, getting information from an SQL database? As opposed to either RTS games, or the Stackexchange model, where information is loaded when the page is loaded, instead of while the page is open.

So would there be any issues with using this model? Would it lag the client's computer or have some problem related?

Best Answer

In general, the overhead involved with AJAX requests is higher. If an Ajax request has, say, 1K of transport overhead, then sending 50 bytes over a single request means that your transfer of actual data is only about 5% efficient.

Ajax requests are typically made to RESTful resources. Socket communication is a persistent connection, and seems more suited for real-time, dashboard-style updates.

Stack Exchange uses a custom socket library to send updates to its pages in real-time, including edit notifications, comment additions and changes to voting tallys.

Related Topic