You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • You can't pipe one big message in I'm afraid - not least because you'd have to be able to parse the stream of JSON.

    You could however send a series of smaller messages? Eg:

    {'command':'writeFile', 
      'fileName': 'test.txt', 
      'fileData':' ... '}
    {'command':'appendFile', 
      'fileName': 'test.txt', 
      'fileData':' ... '}
    {'command':'appendFile', 
      'fileName': 'test.txt', 
      'fileData':' ... '}
    {'command':'appendFile', 
      'fileName': 'test.txt', 
      'fileData':' ... '}
    

    It might help to have an acknowledgement sent back as well, so you can throttle sends.

    Chunking everything into 512 bytes of data would work well since that's the allocation unit size.

    You could also speed things up a bit by sending raw binary data (or even base64 in a string) rather than JSON (which has to be parsed).

    Or, you could probably make things work a bit more smoothly doing something like this:

        ws.on('message', function wsData(msg) { 
            let data = (JSON.parse(msg));
            setTimeout(function() {
             // save file here
            }, 1);
    

    as it wouldn't then block the event loop for quite as long.

About

Avatar for Gordon @Gordon started