How to speed up receiving an HTTP response using jQuery Ajax for large data publishing?

Question: python webapp2 + jQuery Ajax perfroms is extremely bad at getting a response to large text data (takes more than 10 minutes in the opposite direction of 1.7 MB)

Question: What is the reason? how to improve it? could I use any well-established methods to split a large text trunk into small payloads to avoid a browser problem?

Background: I am learning python web programming using webapp2 + Google App Engine. I am trying to create an edit zone what-you-input-is-what-you-see with jQuery Ajax. It is very similar to the postoverflow post editor : wmd-input vs wmd-preview , which offers a real-time preview function. (He continues to indicate β€œdraft saved” in short text. Another example is the editing function in Google Docs mode)

My example works as follows: The jQuery textchange plugin starts an Ajax submission, triggered by every change to the input text field ---> . Python backends receive text and add several messages to it ---> . Send text + messages ---> jQuery uses the server response to update the preview text field (Well, sending the full contents of the received text is for testing purposes only.)

My interface codes:

<script type="text/javascript"> function simpleajax() { $.ajax({ type: 'POST' ,url: '/simpleajax' ,dataType: 'json' ,data:{'esid':'#ajaxin','msgin':$('#ajaxin').val()} ,cache:false ,async:true ,success:function (resp){$('#ajaxout').text(resp.msgout);} ,error:function (jqXHR, textStatus, errorThrown){ {$('#ajaxout').text("Ajax Error:"+textStatus+","+errorThrown)}} }); } $(document).ready(function(){ $('#ajaxin').bind('textchange', function () { $('#ajaxstatus').html('<strong color="blue">Typing ...</strong>'); simpleajax(); }); }); </script> 

My code codes are:

 class simpleajax(BaseReqHandler): def get(self): content={'pagealert':'simpleAjax get response'} self.render_to_response('simpleAjax.html',**content) def post(self): esid=self.POST['esid'] msgin=self.POST['msgin'] msgout="Server noticed element " + esid + " value changed" + " and saved following message: " + msgin content={"msgout":msgout} self.writeout(content) 

Test case and symptoms: Local server + text messages

Copy and paste plain text smaller than 500 KB into the input area: works like a charm. however, 1.7 MB text makes the browser busy for> 10 minutes, which does not respond fully.

Comparison: I paste the same text into the postoffflow post editor, and the preview appears instantly! This time I did not notice the sketches saved in the project. And there are several javascript codes judging the length of the text here. Well. There is a possibility that server communication will not be involved. But this solution is not the solution to my problem. (Google Docs autosave function should use some methods to solve the problem!)

Firebug xhr monitoring results:

 #Request Headers: Host: localhost:8080 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 Accept: application/json, text/javascript, */*; q=0.01 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Content-Type: application/x-www-form-urlencoded X-Requested-With: XMLHttpRequest Content-Length: 2075974 Referer: http://localhost:8080/ajax Cookie: __utma=111872281.1883490050.1319630129.1319630129.1319637523.2; __utmz=111872281.1319630129.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) Pragma: no-cache Cache-Control: no-cache #Response Headers: Server: Development/1.0 Date: Fri, 04 Nov 2011 03:29:05 GMT Cache-Control: no-cache Content-Type: application/json; charset=utf-8 Content-Length: 1790407 #Firebug Timeline: TimePoints TimeElapsed Actions 0 1ms DNS Lookup +1ms 1ms Connecting +2ms 82ms Sending +84ms 1.03s Waiting +1.11s 14m22s Receiving +14m23.11s Done 

Interesting things:

  • jQuery Ajax sends 2MB instead of the 1.7MB payload to the server. What a big overhead! Could this be related to Content-Type: application / x-www-form-urlencoded ?
  • The server takes 1.03s for a response, and jQuery takes 14 minutes to get a response !!!

What is going on behind this? Any help is appreciated! I would like to let the server β€œpush” a lot of things to the client after an Ajax request, but this problem makes it impossible.

+8
jquery python ajax web-applications wysiwyg
source share
1 answer

Using a combination of HTML5 WebSockets and the Worker API, you can asynchronously send / receive data from the server without affecting the performance of the user interface stream.

http://dev.w3.org/html5/websockets/

http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ (the tutorial assumes PHP is a server-side technology)

http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html

Another variant

a) On mousedown and keyup

 - record the cursor position in the text-box - store it in C1. 

b) On textchange

 > record the cursor position - store it in C2. > send only the content between C1 and C2 to your server. Also send the values C1 and C2 to the server, ie your AJAX payload will look something like: { c1: C1, c2: C2, data: <text in the text-box from C1 to C2> } 

You need to see if there is c1 > c2 , and get the substring, respectively, and vice versa

Thus, each time only "changes" are sent to the server, not the full text. But if you copy and paste 5 MB of content, this will not bring any improvement. But for single character changes (like typing, inserting small segments, etc.) this should work pretty well.

+1
source

All Articles