grpc-web icon indicating copy to clipboard operation
grpc-web copied to clipboard

Disconnect/close/cancel from the client side

Open verihelp-vaibhav opened this issue 6 years ago • 8 comments

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.)

verihelp-vaibhav avatar Dec 07 '18 06:12 verihelp-vaibhav

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

stanley-cheung avatar Dec 07 '18 07:12 stanley-cheung

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 avatar Dec 07 '18 07:12 verihelp-vaibhav

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?

verihelp-vaibhav avatar Dec 07 '18 07:12 verihelp-vaibhav

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

longfellowone avatar Apr 06 '19 23:04 longfellowone

+1

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

emil14 avatar Sep 01 '20 11:09 emil14

+1

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

JensErikChristiansen avatar Feb 08 '23 05:02 JensErikChristiansen

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 avatar Feb 08 '23 22:02 sampajano

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

sampajano avatar Feb 08 '23 22:02 sampajano