How to cancel a server side streaming gRPC call from client side in NodeJS?

2.8k Views Asked by At

I have a gRPC server that streams data for an indefinite amount of time. It spawns a child process that retrieves some hardware resources stats.

What I want is to cancel the gRPC call from the client so that I can kill the spawned process. Another solution would be a way to detect on the server when the client disconnects so that I can kill the process.

I am using grpc-node for both the client and the server.

I can't seem to figure out how to do this yet...

1

There are 1 best solutions below

2
On

Found the answer here: https://grpc.github.io/grpc/node/grpc-ClientReadableStream.html Apparently I was using .close instead of .cancel

On Server

call.on('cancelled', () => {
    process.kill();
    console.log('cancelled')
});

On Client

const client = package.runner.Runner(host, grpc.credentials.createInsecure());
const call = client.YourService()
call.on('data', (data) => { console.log(data) })
call.cancel()