grpc / grpc-swift

The Swift language implementation of gRPC.
Apache License 2.0
2.01k stars 413 forks source link

Can we get any callback when Bidirectional stream gets disconnected by the server. #1841

Open araisuki opened 5 months ago

araisuki commented 5 months ago

What are you trying to achieve?

We are connected to our backend service and backend exposes a bidirectional stream which used to for continuous audio streaming. We are facing an problem that this bidirectional stream automatically closes by the backend service by default it gets disconnected if we keep the stream idle for 30 sec(configuration in our backend). So we wont keep the stream idle for more than 30sec. Still in some rare scenario the stream gets disconnect by the backend service, My question is is there any way to client to get to know about the stream disconnection via any callback? When the service disconnects we get a delegate callback, But I am unable get any details on callback option when the stream disconnects.

Here is our backend protos

service Mic { rpc AudioStream(stream MicRequest) returns (stream MicResponse); rpc SendNoteType(NoteType) returns (google.protobuf.Empty); }

Here is the swift code that connects to the stream

mobileAsMicBidiStream = mobileAsMicClient.audioStream(handler: { response in self.serialResponseQueue.async { self.micResponseHandler.handleMicResponse(micResponse: response, stream: self) } })

What have you tried so far?

Tried searching for any callbacks on stream disconnection but couldn't find any details on that.

glbrntt commented 5 months ago

Still in some rare scenario the stream gets disconnect by the backend service, My question is is there any way to client to get to know about the stream disconnection via any callback? When the service disconnects we get a delegate callback, But I am unable get any details on callback option when the stream disconnects.

You can add a callback to the status future which indicates when the RPC has finished. Extending your example this would be something like:

mobileAsMicBidiStream.whenSuccess { status in 
  // ...
}
araisuki commented 5 months ago

@glbrntt The bidirectional stream mobileAsMicBidiStream doesn't expose any method whenSuccess, Are you saying whenever I send a message in mobileAsMicBidiStream using the method sendMessage should we listen for success or failure? I have attached the code below

mobileAsMicBidiStream?.sendMessage(request).whenComplete({

})