title: Sharing SFTP Connection in Azure Functions
body: <p>I have an azure function that sends a file to an SFTP server.  It may be called multiple times so to save time connecting to the SFTP server I would like to reuse the SFTP connection. </p>
  
  <pre><code>const Client = require("ssh2-sftp-client");
  const sftp = new Client();
  let sftpConnected = false;
  
  const sendToSFTP = async (data, location, context) =&gt; {
  
      await setConn(context)
  
      try{
          await sftp.put(data, location);
      }catch(err){
          context.log.error('sftp put error: ' + err);
      }
   }
  
  const setConn  = async (context) =&gt; {
      if (sftpConnected) return
  
      try{
          await sftp.connect({
              host: 'myserver',
              username: 'user',
              passphrase: 'pwd',
          });
          sftpConnected = true;
      }catch(err){
          context.log.error('sftp connect error: ' + err);
      }  
   }
  
  sftp.on('close', () =&gt; {
      sftpConnected = false;
      sftp.end();
  });
  sftp.on('end', () =&gt; {
      sftpConnected = false;
      sftp.end();
  });
  
  exports.sendToSFTP = sendToSFTP;
  </code></pre>
  
  <p>This works when testing locally but when deployed to Azure the close and end listeners do not seem to be called when the Azure function goes idle.  When I try to use the sftp connection after a period of time there is no connection there and the call to the sftp server times out.</p>
  
  <p>Has anyone solved this issue?</p>