You are reading a single comment by @thebells and its replies. Click here to read the full conversation.
  • I have a websocket handler that writes a message to a file with the function below. However, with a large message, saving the file can take a long time. Is there a way to pipe the the websocket message directly to the file, or to speed up how long it takes to receive and save it?

    var fs = require('fs');
    
    function wsHandler(ws) {
        ws.on('message', function wsData(msg) { 
         
          try {
            let data = (JSON.parse(msg));
            /*data would be this:
    
                 data = {'command':'saveFile', 
                         'fileName': 'test.txt', 
                         'fileData':'This is a test of the file data, 
                                     but imagine it is 4KB of information.'
                         }
            */
            try {
              switch(data.command) {
    
                case 'saveFile':
                  fs.writeFileSync(data.fileName, data.fileData);
                  break;
                
                
               }
            }
            catch(err){} 
           
          }
          catch(err){}
        });
    
About

Avatar for thebells @thebells started