4

I am using nodejs grpc server streaming to deliver realtime data from server to the clients. As the clients want to listen to the data, they should call a server function which returns a server stream. Then if the client want to end the listening I guess it should call the end method on the call object. But unfortunately I couldn't find any clue on the documentation about how to do this. I have tried call.end and call.destroy and call.cancel but end and destroy did nothing and cancel throws the following error. The sample client side code is as following:

function getData(token, userId) {
    const data =  {
        token,
        userId
    }
    let call = DataService.getData(data)
    call.on('data', res => {
        console.log(res)
    })
    call.on('status', console.log);
    call.on('error', console.log);
    setTimeout(() => {
        
        console.log('destroy')

        call.cancel()
    }, 5000)
}

and the error is:

Error: 1 CANCELLED: Cancelled on client
    at Object.callErrorFromStatus (...grpc\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
    at Object.onReceiveStatus (...grpc\node_modules\@grpc\grpc-js\build\src\client.js:330:49)
    at Object.onReceiveStatus (...grpc\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:299:181)
    at ...grpc\node_modules\@grpc\grpc-js\build\src\call-stream.js:145:78
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  code: 1,
  details: 'Cancelled on client',
  metadata: Metadata { internalRepr: Map(0) {}, options: {} }
}
  • You say that it is throwing this error. Are you sure you're not actually seeing the output of `console.log` in the `error` event handler? – murgatroid99 May 18 '21 at 17:57

0 Answers0