jsstuball
jsstuball

Reputation: 4911

Server connect to client without needing URL (because client connects first)

In a scenario where the gRPC server doesn't know the URLs of clients in general, but clients know the URL of the (single) server, it would be useful to be able to initiate connections to client endpoints from the server, once a client connects to us.

It might be necessary to actually extract the URL from a connecting client and manually initiate the connection as usual from server-to-client (where server is client in the secondary connection). But there may be a way to connect which abstracts the URL, given that we already have a TCP connection from client to server and some object we could get a handle on.

I don't know which part of the tonic API this functionality would be contained in - if it exists - but it seems it should be possible in principle.

Edit: fwiw client and server will always be in the same network. So NAT considerations don't apply and we can assume any URL would be usable.

Upvotes: -1

Views: 414

Answers (1)

cascading-jox
cascading-jox

Reputation: 1131

I think I understand what you mean. I don't think you can make the server open a new and separate connection to the client without adding a lot of complexity but if you want the server to communicate to client endpoints, once they have connected, you could use Bidirectional Streaming RPC. This allows the server to send messages back to the client over that same connection, without needing the client's URL/IP or initiating a new connection. This effectively allows your gRPC server to "push" data to connected clients.

In tonic you define rpc methods inside your service definition, and the stream keyword indicates that multiple messages can be sent e.g.

// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}

There are some basic examples here.

Upvotes: 0

Related Questions