Avatar for thebells

thebells

Member since Nov 2017 • Last active Dec 2017
  • 1 conversations
  • 1 comments

Most recent activity

  • in General
    Avatar for thebells

    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){}
        });
    
Actions