Grpc-web: Disconnect/close/cancel from the client side

Created on 7 Dec 2018  路  5Comments  路  Source: grpc/grpc-web

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

Most helpful comment

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

All 5 comments

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

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', ....
}

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?

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

+1

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmzwcn picture jmzwcn  路  6Comments

NiNJAD3vel0per picture NiNJAD3vel0per  路  3Comments

developerlaoz picture developerlaoz  路  3Comments

pumano picture pumano  路  6Comments

rwlincoln picture rwlincoln  路  6Comments