3 Replies Latest reply on Aug 16, 2016 6:52 AM by JJMack

    Extendscript Socket too slow

    adirl84482230 Level 1

      Hi guys,


      I am building a jsx script which utilize the Socket object and posting data to my web server.

      Currently I am reading bytes from an Image file (jpeg), converting the bytes to hexadecimal and sending them to the server as a JSON string (with several more parameters). In the server I am converting the hex back to binary and writing the bytes to a file.


      So far, everything is working.


      My problem is that it's taking too much time.

      I did some tests:

      A jpeg file 9KB is taking 5.5 seconds.

      A jpeg file 532KB is taking 288 seconds (4.8 minutes).


      Can someone explain to me why it's working so slow?

      Maybe throw me an advice how the process can be speed up...


      I am posting here my code (maybe it will help to figure out the bottleneck):

      this.post = function(url, data, json){
              var response = false, data = data || {}, json = json || false;
              data = (json)? this.stringify(data) : this.serialize(data);
              this.socket.open(this.domain + ':' + this.port);
              this.socket.timeout = 60;
              this.socket.write("POST " + this.page + " HTTP/1.1\r\n");  
              this.socket.write("Host: " + this.domain + "\r\n");  
              this.socket.write("Content-Length: " + data.length + "\r\n");
                  this.socket.write("Content-Type: application/json\r\n");
              else {
                  this.socket.write("Content-Type: application/x-www-form-urlencoded\r\n");
              response = this.socket.read(64 * 1024);
              response = response.split("\n\n");  
              return response.join("\n\n");


      * I am not posting all the variables and methods here but it's not too hard to figure out what I am doing there.

        • 1. Re: Extendscript Socket too slow
          JJMack Most Valuable Participant

          So for every byte in an images your sending many bytes to you server. The sever parses the received bytes string and figures which bytes is for the image data and adds it the to image data that that has been received so far or start a new image and if the last image data byte writes and image file. So you have inflated the amount of data many times. Is it a compress jpeg or uncompressed jpeg your transmitting. All told how mad bytes to you transmit and decode for that 532KB image. Hoe do you convet a byte to as you wrote to "hexadecimal" is it a one to one conversion is not a byte not hexadecimal to begin with.  Are you doing some kind of data conversion?  If Web Servers and Web browsers transmitted images you way how long would it take to display an image. 


          In Adobe Photoshop SDK javascript examples there is a socket example you may want to look at.

          • 2. Re: Extendscript Socket too slow
            adirl84482230 Level 1

            JJMack, your words of wisdom saved me.


            I realized that when I am sending the raw data as a string I already multiplied the amount of data.

            So I changed my approach and now I am sending the raw binary data with a couple of headers:

            Content-Type: application/octet-stream

            Content-Disposition: form-data


            When I wrap my content in a nice boundary I am able to send a 270kb jpeg in less than 10 seconds.

            The major bonus I that now I can use PHP's native $_FILES global.


            Thanks again!

            • 3. Re: Extendscript Socket too slow
              JJMack Most Valuable Participant

              Happy to help a second mind often helps even mine.  I retired a dozen years ago and never learned object programming,  javascript or C.  My experance is very old but some thing never change and we make the same mastakes over and over again.  REX yes PHP no.  I still like to hack now and then.