Repository navigation
Expand file tree
/
Copy pathserver-push.html
More file actions
198 lines (187 loc) · 10.5 KB
/
Copy pathserver-push.html
File metadata and controls
198 lines (187 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
layout: base
project: remoc
title: "Bidirectional RPC in Rust: server push and client callbacks"
description: >-
Make bidirectional Rust RPC calls over one connection: return a channel for streamed
updates or pass a client callback. Follow the existing counter and pizzeria examples.
social_description: >-
RPC where a call can hand over a channel and the server can call the client back,
over the one connection the client opened.
canonical: /server-push.html
faq:
- q: "Does server push work when the client is behind NAT?"
a: >-
Yes. Everything runs over the one connection that the client opened, and channels
and calls work in both directions on it. The server never has to connect to the
client, so NAT and firewalls on the client side are no obstacle.
- q: "How is this different from gRPC server streaming?"
a: >-
A gRPC stream is tied to the method that declared it. A Remoc channel is a value:
a call can return it, a message can carry it, and it can be stored and used long
after the call completed. A gRPC bidirectional stream carries messages in both
directions within a client-initiated call; it does not by itself let the server
invoke a client-defined RPC method. With Remoc the client can also pass a remote
function or serve a trait of its own over the existing connection.
- q: "Can an RPC method return a channel or stream of updates?"
a: >-
Yes. Return an <code>rch::mpsc::Receiver</code> for a sequence or an
<code>rch::watch::Receiver</code> for the latest state. The caller receives the
endpoint as a normal result and continues receiving updates after the RPC
method returns. Both patterns are implemented in the counter example.
- q: "What happens when the client disconnects?"
a: >-
Sending into a channel whose remote end is gone fails with an error, and calls on
a disconnected client fail likewise. The server notices at the point of use and
can drop its state for that client.
- q: "Can the server push without the client asking first?"
a: >-
The client has to hand the server a way in once, by calling a method that returns
a channel, by including a sender in a message or by providing a client for its
own trait. From then on the server pushes whenever it wants.
- q: "Can I use this between Rust and another language?"
a: >-
No. Remoc's protocol is defined by Rust types serialized with Serde, so both
peers must be Rust programs. For cross-language services a schema-based system
such as gRPC is the better fit.
---
<header class="page-header">
{% include nav.html %}
<div class="wrap">
<h1>Bidirectional Rust RPC over one connection</h1>
<p class="lead">
Classical RPC is request and response: the client asks and the server answers.
But often the server has something to say on its own, for example a finished
job, a changed value or an incoming event. <a href="/">Remoc</a> lets an
RPC return a channel for server push or accept a client callback. Requests,
streamed updates and reverse-direction calls share the same connection;
the server does not need a second connection back to the client.
</p>
</div>
</header>
<main id="content">
<section class="section">
<div class="wrap narrow">
<h2>The limits of request and response</h2>
<p>
In a request/response framework the server cannot start a conversation.
Polling wastes requests and adds latency.
</p>
<p>
<strong>Compared with gRPC:</strong> Server-streaming methods cover
subscriptions, but each stream is tied to the method that opened it and
cannot be passed around. The server still cannot invoke anything on the
client.
</p>
<p>
A second connection in the reverse direction requires the client to be
reachable, which fails as soon as it sits behind NAT or a firewall.
</p>
</div>
</section>
<section class="section alt">
<div class="wrap narrow">
<h2>Callbacks and transferred channel endpoints</h2>
<p>
In Remoc, channel endpoints are values that calls can return and accept. A
method returns an <code>rch::mpsc::Receiver</code> and the client has a
subscription; it returns an <code>rch::watch::Receiver</code> and the client
always sees the latest value; the client puts a sender into a request and
the server reports progress into it. Each channel keeps working after the
call that created it has returned.
</p>
<p>
For callbacks, choose the shape that fits your interface:
</p>
<ul>
<li><strong>A remote function</strong> wraps a closure with
<code>rfn::RFn</code>. Pass it to the server, which calls it with
typed arguments.</li>
<li><strong>A client-served trait</strong> exposes several related
methods. Implement a trait carrying <code>#[rtc::remote]</code>
on the client and pass its generated client object to the server.</li>
</ul>
<p>
<strong>Compared with gRPC:</strong> Bidirectional streaming exchanges
messages in both directions within a streaming method. Remoc supports
that through channels, but a callback is a separate callable value with
its own arguments and result. The server invokes it on demand over the
existing connection, without the client polling for callback requests.
</p>
</div>
</section>
<section class="section">
<div class="wrap">
<h2 id="return-channel">Return a channel from an RPC method</h2>
<p class="section-lead">
This trait is from the <a href="/examples/rtc.html">counter example</a>. The
<code>watch</code> method returns a watch channel receiver, giving the
client notifications when the value changes. Its
<code>count_to_value()</code> method similarly returns an mpsc receiver
for a sequence. Use watch for the latest state; use mpsc when the
receiver must read each item in a stream.
</p>
<figure class="code-single">
<figcaption>Service definition</figcaption>
<pre><code><span class="a">#[rtc::remote(server(SharedMut))]</span>
<span class="k">pub</span> <span class="k">trait</span> Counter {
<span class="c">/// Obtain the current value of the counter.</span>
<span class="k">async</span> <span class="k">fn</span> <span class="f">value</span>(&<span class="k">self</span>) -> <span class="t">Result</span><<span class="t">u32</span>, rtc::CallError>;
<span class="c">/// Watch the current value of the counter for immediate</span>
<span class="c">/// notification when it changes.</span>
<span class="k">async</span> <span class="k">fn</span> <span class="f">watch</span>(&<span class="k">mut</span> <span class="k">self</span>)
-> <span class="t">Result</span><rch::watch::Receiver<<span class="t">u32</span>>, rtc::CallError>;
}</code></pre>
</figure>
<p class="section-foot">
<a class="button" href="/examples/rtc.html">Complete trait calling example
→</a>
<a class="button" href="/#pipelining">Pipelined calls save the round trips
→</a>
</p>
</div>
</section>
<section class="section alt">
<div class="wrap narrow">
<h2 id="client-callback">Server-to-client RPC callbacks: the pizzeria example</h2>
<p>
The <a href="/examples/tracing.html">pizzeria example</a> uses the
remote-function option. Its shared <code>Progress</code> type is a
<code>rfn::RFn<(String,), ()></code>: it accepts a progress message
and returns no value.
</p>
<ol>
<li>The <a href="/examples/tracing.html#pizzeria-client-src-main-rs">client
wraps its progress handler</a> with <code>Progress::new_1</code>
and passes it to <code>order()</code>.</li>
<li>After each preparation step, the
<a href="/examples/tracing.html#pizzeria-server-src-main-rs">server calls
<code>progress.try_call(...)</code></a> with a progress message.</li>
<li>The supplied closure runs back in the client process while the order
runs on the server.</li>
</ol>
<p>
Both directions use the one TCP connection opened by the client.
The example includes all
source files and run commands; OpenTelemetry export is optional.
</p>
<p class="section-foot">
<a class="button" href="/examples/tracing.html#running">Run the client
callback example →</a>
</p>
</div>
</section>
{% include faq.html %}
<section class="section">
<div class="wrap narrow">
<h2>Related pages</h2>
<p>
<a href="/comparison.html">Remoc vs. other RPC libraries</a> compares this
model with tarpc, gRPC and Cap'n Proto.
<a href="/remote-channels.html">Channels between processes and machines</a>
explains the channels used for server push. For the local case of a daemon
notifying clients, see <a href="/ipc.html">IPC between Rust processes</a>.
</p>
</div>
</section>
</main>