1

Following this information:

I was hoping to achieve the following:

  • AJAX -> (POST)PHP(socket) -> (socket)Python

By doing:

$.ajax({
    type: "POST",
    url: "post.php",
    async: true,
    cache: false,
    data: { data : 'ping 127.0.0.1' },
    success: function(msg) {
        console.log(msg);
    }
});

With PHP looking like:

//header("Connection: close");
//header("Content-Type: application/octet-stream");
socket_write($socket, 'ping 127.0.0.1', strlen('ping 127.0.0.1'));
ignore_user_abort();
//ob_implicit_flush(true);
//ob_end_flush();
while ($out = socket_read($socket, 8192)) {
    echo $out;
    ob_flush();
}
//ob_start();

And Python simply doing:

sock.recv(8192)
for i in range(0, 4):
    sock.send(bytes(json.dumps('Ping successfull..'), 'UTF-8'))
    sleep(0.5)

I thought I'd be able to buffer out the data in "real-time" instead of ending up with the data bunched up like this:

"Ping successfull..""Ping successfull..""Ping successfull..""Ping successfull.."

Where did i go wrong in my thinking? This should be quite straight forward? I've done it so many times but yet this time I can't figure out what i'm doing wrong :/

I've also verified that it is indeed the AJAX/PHP bridge not working, PHP recieves the data as it arrives, verified with strftime("%H:%M:%S") and i get the expected difference in between each recieves.

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131

1 Answers1

1

AJAX uses HTTP, so the callback for success/failure is only called when the request completes and there are no options for getting the data while it's being received. You should check out web sockets, which will allow you to handle streaming data from the server.


Edit: You could also look at multipart/x-mixed-replace and EventSource

sakai
  • 163
  • 7
  • You did see that he mentioned sockets in his post, right? – Jay Blanchard May 01 '14 at 14:22
  • @JayBlanchard Saki is pointing me to web-sockets, sort of like socketio which (to me) is horrible. HTTP supports streaming data, and i'm quite sure Ajax do to. Probably just me using it the wrong way again.. been a long night.. – Torxed May 01 '14 at 14:27
  • Solution is here i guess, just.. gotta go through the relevant stuff: http://ajaxpatterns.org/HTTP_Streaming - I thought i remembered correctly when i thought `$.ajax({... success: ...})` contained the `responseText` variable which is the buffer from the server as is without the connection reaching an end. But I guess jQuery changed that along the way o0 – Torxed May 01 '14 at 14:29
  • 1
    If you get closer to the metal than jQuery and access the XHR object directly, you can read partial responseText as it's coming in, but you should be aware that the value for responseText while the request is still ongoing is not well defined, and some older browsers will not provide any value here at all until the request completes. Websockets / Socket.IO is the right solution, you can come up with something hacky with XHR, but it will never work as consistently or reliably as a purpose-built technology. – MightyE May 01 '14 at 15:08