Grpc-node: Don't know what to write here really.

Created on 22 May 2020  路  8Comments  路  Source: grpc/grpc-node

Connection is closing before i write response to it.

First of all here is my GRPC-Java Client.

StreamObserver<Request> requestObserver = asyncStub.status(new StreamObserver<Reply>() {
...
Observer... on next on error on completed part here. }
...
try catches surrounded below in actuality.
requestObserver.onNext(newMessage(message));
requestObserver.onCompleted();

My GRPC-Node Server

call.on('data', () => { do_something(); console.log('Data'); call.write(something); })
call.on('end', () => { console.log('End'); call.end(); } )

I put System.out.println("Completed") on requestObserver's onCompleted. So i get a message when it completes.

Then i put console.log() on node's each call.on just before call.write and call.end

Problem is, connection is closing before the call.write(). I see End message first.

It seems like call.on('end', ()) is working before call.on('data', ())

:( I didn't understand anything.

All 8 comments

Is your do_something function asynchronous?

yis.

What's probably happening here is that the data event is triggered, then you call do_something, then the end event triggers and you see the End output, then do_something finishes and you see the Data output. You can test this by moving the Data output to before you call do_something.

I'm not sure, but I think you may be able to do that by calling call.pause() before calling do_something(), and calling call.resume() after writing the response. This will ensure that after you get each data event, you won't get another one until you have finished processing the previous one. You probably want to do that anyway, to ensure that you send responses in the same order that you get the requests.

If that doesn't help you will need to find another way to track which requests you have responded to, and only call call.end() when you have sent all of the responses.

do i need to call.end if i return the last call.write() ?

for example:

call.write
call.write
return call.write();

Actually i tried with not ending calls and it's working ok. but im not sure what it costs.

return call.write() doesn't do anything. call.end is how you say that you're done writing, so you need to call it.

i put call.pause before async function and call.resume after async function.

now it's working ok.

i think it should be documented somehow as it's very normal to use async database thingies to process incoming data.

+infinite thanks.

Everything I said applies to built in Node streams in general, and how they interact with async functions. None of it is specific to gRPC.

yes but documentation still would be helpful for those having brain inflamation like me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Slapbox picture Slapbox  路  5Comments

Harmonickey picture Harmonickey  路  6Comments

tvk-codecraft picture tvk-codecraft  路  3Comments

samuela picture samuela  路  4Comments

polRk picture polRk  路  3Comments