title: Access grpc stream variable for long-running process in Node
body: <p>I am using Node.js to connect to a server using <a href="http://www.grpc.io/" rel="nofollow">gRPC</a> that performs a long running task.</p>
  
  <p>The server sends a unidirectional stream to the client (the Node.js app) while the job is in progress. I need to implement a Stop button and am told that closing the <a href="http://www.grpc.io/docs/tutorials/basic/node.html#streaming-rpcs" rel="nofollow">gRPC stream</a> will stop the job in progress.</p>
  
  <p>This is currently my code:</p>
  
  <pre class="lang-js prettyprint-override"><code>let express = require('express'),
      router = express.Router(),
      grpc = require('grpc'),
      srv = grpc.load(__dirname + '/job_handler.proto').ns;
  
  let startJob = (jobID, parameters) =&gt; srv.createJob(jobID, parameters);
  
  router.post('/jobs', (req, res) =&gt; {
      let lengthyOperation = startJob(jobID, parameters);
      lengthyOperation.on('data', (data) =&gt; {
          console.log(`Data from lengthy operation: ${data}`);
      });
  
      lengthyOperation.on('end', () =&gt;
          console.log('Lengthy operation completed');
      });
  
      res.setHeader('Location', `/jobs/${jobID}`);
      res.status(202).send();
  });
  </code></pre>
  
  <p>As you can see, I send an <a href="https://httpstatuses.com/202" rel="nofollow">HTTP 202</a> response to the client upon creating the job and it continues asynchronously in the background.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>How do I close the stream?</li>
  <li>How do I access the <code>lengthyOperation</code> variable to do so?</li>
  </ol>