grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.66k stars 764 forks source link

Disconnect/close/cancel from the client side #406

Open verihelp-vaibhav opened 5 years ago

verihelp-vaibhav commented 5 years ago

Hi, I can't find any method to disconnect the client as per my command. Using an example snippet here -

function doSomething () {
    var SomeServiceClient = xxx.yyy_service.SomeServiceClient;
    var client = new SomeServiceClient('http://localhost:8080', null, null);
    status = client.testme(parameters, {},function (err, response) {
        console.log('testme response: ' + response);
    });
    // how do i disconnect the client?
}

The problem I'm encountering is - user clicks a button, I make a request to the gRPC server and start receiving data in a stream. If the user clicks again, I start receiving data again in another stream. I want to disconnect any previous streams/connections so that I can receive only one stream at a time. Any help/suggestions would be greatly appreciated! (I know I could disable the click till the stream is finished, but that's not what I'm looking for.)

stanley-cheung commented 5 years ago

In your snippet above, you can call status.cancel().

verihelp-vaibhav commented 5 years ago

Okay, I found the way to cancel the previous stream. But still not sure how to disconnect my client completely. For anyone wondering how to cancel the previous stream, here's a sample -

var stream;
function getStream(request) {
    if (stream) {
        stream.cannel();
    }
    stream = SomeServiceClient.getMyStream(request, {});
}

function myFuncThatUsesStream(parameters) {
    getStream();
    // do something with stream now
    stream.on('data', ....
}
verihelp-vaibhav commented 5 years ago

In your snippet above, you can call status.cancel().

Thanks @stanley-cheung, I knew about the cancel but didn't have the object from previous call to cancel. I figured it out and pasted my code above.

Do you have any idea about completely disconnecting the client?

longfellowone commented 5 years ago

How do you cancel a request when using the promise client? as the client.request only returns a promise?

emil14 commented 4 years ago

+1

Need to know how to cancel using the promise client. Please provide some docs

JensErikChristiansen commented 1 year ago

+1

I'd like to know how to cancel a promise client, too.

sampajano commented 1 year ago

Hi! Thank for asking!

This feature was requested in https://github.com/grpc/grpc-web/pull/1240 and i'm planning on adding a new option for this. Will update the PR when it's done.

Thanks!

sampajano commented 1 year ago

Re-opening this issue for the Promise client use case.