tomloprod tomloprod - 1 year ago 64
Javascript Question

Reduce AJAX request size. Simple chat with Polling system

NOTICE: I replaced my polling system with
but I still want to know the answer to my questions above.

I'm trying to reduce an AJAX request of a traditional-polling message system, but I don't know how to get it:

$chatbox = $("#chatbox");

// I send the sha1 of the chatbox html content to verify changes.
$.post("post.php", {checksum: hex_sha1($chatbox.html())}, function (data, status) {

switch (status) {
case "success":
// If version of "post.php" checksum is different than mine (there are changes) data isn't empty; I assign data as the new content of the chatbox.

$chatbox.html('Connection error...');
}, 1000);

Well, As you see I use an
miliseconds as parameter and thanks to the
checksum system I can reduce the size of all AJAX response to
343 B
(except when "post.php" returns some new message, obviously)


  • Why all my AJAX requests have ever the same size (
    343 B)
    even though I change the SHA1 (
    20 B
    ) hash to MD5 (
    16 B

  • My checksum variable (SHA1) occuppies
    20 B
    : Where do the remaining
    323 B

  • Could I reduce more the AJAX request size? How?


is a implementation of SHA1 algorithm for Javascript:


Unfortunately I can't use an Server-Push Technique like
. I can only use Javascript and PHP.

Answer Source

Why not use the plain javascript AJAX Request? Maybe your AJAX data is too long, that's why it has a large size: and the only thing you can do for it is to make the AJAX data have a few data.

What do you want? like Facebook AJAX Polling? Do it like this on the server PHP:

$chat_data = "(this is the chat data variable if there is no chat data it will idle)";
while (!$chat_data) {
     // when there's no chat data let's idle the request without disconnecting
     // the client from the AJAX request.

On JavaScript Client Side:

function repoll () {
     chat_poll = new XMLHttpRequest();
     // the chat_req variable is for sending POST data to the server.
     chat_req = new FormData();
     chat_req.append("do", "chatpoll");"POST", "post.php");
     chat_poll.onload = function () {
     // do something here with the chat data
     // repoll the server


By doing this, your implementing the Facebook like server polling.

For the websocket example in JavaScript client side:

web_socket = new WebSocket("ws://[thesocket]:[theport]");
web_socket.onmessage = function (w) {
     // do something here. this will fire if messages is received from the websocket.
     // you will get the message from variable.
     alert("Data Received: " +;

// to send data to the web socket do this:
web_socket.send("the data you want. I prefer JSON for sending configuration and chat data or XML if you want");