How can I call client functions from server? #3943
-
What I need is bidirectional RPC in which I can call client functions from server as well as calling server from client. Is there a solution other than mixing client and server code? |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 1 reply
-
Hi Kamyar, With IceRPC we prefer the term "server-to-client" or "the other way around" to refer to RPCs initiated by the server, instead of using "bidirectional", with that out of the way let me explain how this works with IceRPC. With IceRPC you send a request by using an invoker, and you process a request and return the corresponding response by using a dispatcher. On the client side, you typically use the ClientConnection or ConnectionCache invokers for sending requests to a server, and that is what most examples show:
Here we create a client connection, and we use it as the invoker of the proxy, nothing fancy. Sending a request the other way around is the same, you just need to use a different invoker. In this case, you would want to use the invoker provided by the connection context. This context is made available to the dispatcher through the IDispatchInformationFeature, the Router's UseDispatchInformation extension method adds a middleware to the dispatch pipeline that adds this feature to the requests it dispatches. With the router correctly configured you can access this invoker using something like:
The client configured dispatcher will receive the request, you configure the client dispatcher with the Dispatcher property of the connection options when you create the client connection. You might be interested in the new Thermostat example, which shows with real code how all this works. |
Beta Was this translation helpful? Give feedback.
Hi Kamyar,
With IceRPC we prefer the term "server-to-client" or "the other way around" to refer to RPCs initiated by the server, instead of using "bidirectional", with that out of the way let me explain how this works with IceRPC.
With IceRPC you send a request by using an invoker, and you process a request and return the corresponding response by using a dispatcher.
On the client side, you typically use the ClientConnection or ConnectionCache invokers for sending requests to a server, and that is what most examples show: