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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Async Rust client for the #yxjw Cloudflare signaling endpoint (#s0fc).
//!
//! This lets a Rust project (e.g. agent-doc) depend on lazily-rs for
//! distributed peer discovery: connect to the signaling Worker over a
//! WebSocket, join a session, learn the roster, and exchange the WebRTC
//! SDP/ICE handshake (or relay opaque payloads) to reach other peers.
//!
//! The wire protocol is the single source of truth shared with the TypeScript
//! client (`signaling/`); see `SPEC.md` → *Signaling wire protocol*. The
//! [`ClientMessage`] / [`ServerMessage`] enums are `serde`-tagged to match that
//! protocol byte-for-byte (`PeerId` serializes as a bare JSON number, matching
//! the TS `number` peer id), and `signaling_protocol_conformance` tests assert
//! the exact JSON shapes.
//!
//! Enabled by the `signaling-client` feature.
use crate::distributed::PeerId;
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio_tungstenite::tungstenite::Message;
/// A message the client sends to the signaling server.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case", deny_unknown_fields)]
pub enum ClientMessage {
/// Join the session as `peer`, optionally advertising capabilities.
Join {
peer: PeerId,
#[serde(skip_serializing_if = "Option::is_none", default)]
capabilities: Option<Vec<String>>,
},
/// WebRTC offer for `to`.
Offer { to: PeerId, sdp: String },
/// WebRTC answer for `to`.
Answer { to: PeerId, sdp: String },
/// ICE candidate for `to`.
Ice { to: PeerId, candidate: String },
/// Opaque application payload (e.g. a CRDT delta) relayed to `to`.
Relay { to: PeerId, payload: Value },
/// Leave the session.
Leave,
}
/// A message the signaling server sends to the client.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case", deny_unknown_fields)]
pub enum ServerMessage {
/// Sent on join: this peer's id and the current roster (excluding self).
Welcome { peer: PeerId, peers: Vec<PeerId> },
/// Another peer joined.
PeerJoined { peer: PeerId },
/// Another peer left.
PeerLeft { peer: PeerId },
/// Forwarded WebRTC offer from `from`.
Offer { from: PeerId, sdp: String },
/// Forwarded WebRTC answer from `from`.
Answer { from: PeerId, sdp: String },
/// Forwarded ICE candidate from `from`.
Ice { from: PeerId, candidate: String },
/// Forwarded opaque payload from `from`.
Relay { from: PeerId, payload: Value },
/// Server-side error (e.g. `permission_denied`, `unknown_target`).
Error { code: String, message: String },
}
/// Errors surfaced by [`SignalingClient`].
#[derive(Debug)]
pub enum SignalingError {
/// WebSocket transport error.
WebSocket(tokio_tungstenite::tungstenite::Error),
/// JSON encode/decode error for a protocol frame.
Protocol(serde_json::Error),
/// The server closed the connection.
Closed,
}
impl std::fmt::Display for SignalingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SignalingError::WebSocket(e) => write!(f, "signaling websocket error: {e}"),
SignalingError::Protocol(e) => write!(f, "signaling protocol error: {e}"),
SignalingError::Closed => write!(f, "signaling connection closed"),
}
}
}
impl std::error::Error for SignalingError {}
impl From<tokio_tungstenite::tungstenite::Error> for SignalingError {
fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
SignalingError::WebSocket(e)
}
}
impl From<serde_json::Error> for SignalingError {
fn from(e: serde_json::Error) -> Self {
SignalingError::Protocol(e)
}
}
impl ServerMessage {
/// Decode and validate an untrusted server frame.
///
/// Serde enforces the closed tagged union and exact per-variant fields;
/// this method additionally enforces semantic constraints that JSON Schema
/// cannot express, such as a welcome roster excluding the joining peer.
pub fn from_json_slice(bytes: &[u8]) -> Result<Self, serde_json::Error> {
let message: Self = serde_json::from_slice(bytes)?;
if let Self::Welcome { peer, peers } = &message
&& peers.contains(peer)
{
return Err(serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"welcome roster must exclude the joining peer",
)));
}
Ok(message)
}
}
type WsStream =
tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;
/// A connected signaling-session client.
///
/// ```no_run
/// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
/// use lazily::{PeerId, SignalingClient, ServerMessage};
///
/// let mut client = SignalingClient::connect("wss://signaling.example.com", "room-1", PeerId(1)).await?;
/// while let Some(msg) = client.recv().await {
/// match msg? {
/// ServerMessage::Welcome { peers, .. } => println!("roster: {peers:?}"),
/// ServerMessage::PeerJoined { peer } => {
/// client.relay(peer, serde_json::json!({ "hello": true })).await?;
/// }
/// _ => {}
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub struct SignalingClient {
ws: WsStream,
peer: PeerId,
}
impl SignalingClient {
/// Connect to `{base_url}/session/{session}` and join as `peer`.
///
/// `base_url` is the Worker origin, e.g. `wss://signaling.example.com`.
pub async fn connect(
base_url: &str,
session: &str,
peer: PeerId,
) -> Result<Self, SignalingError> {
Self::connect_with_capabilities(base_url, session, peer, None).await
}
/// Connect and join, advertising `capabilities` to other peers.
pub async fn connect_with_capabilities(
base_url: &str,
session: &str,
peer: PeerId,
capabilities: Option<Vec<String>>,
) -> Result<Self, SignalingError> {
let url = format!("{}/session/{}", base_url.trim_end_matches('/'), session);
let (ws, _response) = tokio_tungstenite::connect_async(&url).await?;
let mut client = Self { ws, peer };
client
.send(&ClientMessage::Join { peer, capabilities })
.await?;
Ok(client)
}
/// This client's peer id.
pub fn peer(&self) -> PeerId {
self.peer
}
/// Send a protocol message to the server.
pub async fn send(&mut self, message: &ClientMessage) -> Result<(), SignalingError> {
let json = serde_json::to_string(message)?;
self.ws.send(Message::Text(json)).await?;
Ok(())
}
/// Send a WebRTC offer to `to`.
pub async fn offer(
&mut self,
to: PeerId,
sdp: impl Into<String>,
) -> Result<(), SignalingError> {
self.send(&ClientMessage::Offer {
to,
sdp: sdp.into(),
})
.await
}
/// Send a WebRTC answer to `to`.
pub async fn answer(
&mut self,
to: PeerId,
sdp: impl Into<String>,
) -> Result<(), SignalingError> {
self.send(&ClientMessage::Answer {
to,
sdp: sdp.into(),
})
.await
}
/// Send an ICE candidate to `to`.
pub async fn ice(
&mut self,
to: PeerId,
candidate: impl Into<String>,
) -> Result<(), SignalingError> {
self.send(&ClientMessage::Ice {
to,
candidate: candidate.into(),
})
.await
}
/// Relay an opaque payload (e.g. a CRDT delta) to `to`.
pub async fn relay(&mut self, to: PeerId, payload: Value) -> Result<(), SignalingError> {
self.send(&ClientMessage::Relay { to, payload }).await
}
/// Announce departure and close.
pub async fn leave(&mut self) -> Result<(), SignalingError> {
self.send(&ClientMessage::Leave).await?;
self.ws.close(None).await?;
Ok(())
}
/// Receive the next server message. Returns `None` once the connection
/// closes; control frames (ping/pong/close) are handled internally.
pub async fn recv(&mut self) -> Option<Result<ServerMessage, SignalingError>> {
loop {
match self.ws.next().await? {
Ok(Message::Text(text)) => {
return Some(
ServerMessage::from_json_slice(text.as_bytes())
.map_err(SignalingError::from),
);
}
Ok(Message::Binary(bytes)) => {
return Some(
ServerMessage::from_json_slice(&bytes).map_err(SignalingError::from),
);
}
Ok(Message::Close(_)) => return None,
// Ping/Pong/frame: keep waiting for an application frame.
Ok(_) => continue,
Err(e) => return Some(Err(SignalingError::WebSocket(e))),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
// Conformance: these exact JSON shapes are the contract shared with the
// TypeScript client/Worker (`signaling/src/protocol.ts`).
#[test]
fn client_messages_match_wire_format() {
assert_eq!(
serde_json::to_value(ClientMessage::Join {
peer: PeerId(1),
capabilities: None
})
.unwrap(),
json!({ "type": "join", "peer": 1 })
);
assert_eq!(
serde_json::to_value(ClientMessage::Join {
peer: PeerId(1),
capabilities: Some(vec!["crdt".into()]),
})
.unwrap(),
json!({ "type": "join", "peer": 1, "capabilities": ["crdt"] })
);
assert_eq!(
serde_json::to_value(ClientMessage::Offer {
to: PeerId(2),
sdp: "x".into()
})
.unwrap(),
json!({ "type": "offer", "to": 2, "sdp": "x" })
);
assert_eq!(
serde_json::to_value(ClientMessage::Ice {
to: PeerId(2),
candidate: "c".into()
})
.unwrap(),
json!({ "type": "ice", "to": 2, "candidate": "c" })
);
assert_eq!(
serde_json::to_value(ClientMessage::Relay {
to: PeerId(2),
payload: json!({"d": 1})
})
.unwrap(),
json!({ "type": "relay", "to": 2, "payload": { "d": 1 } })
);
assert_eq!(
serde_json::to_value(ClientMessage::Leave).unwrap(),
json!({ "type": "leave" })
);
}
#[test]
fn server_messages_match_wire_format() {
assert_eq!(
serde_json::from_value::<ServerMessage>(
json!({ "type": "welcome", "peer": 1, "peers": [2, 3] })
)
.unwrap(),
ServerMessage::Welcome {
peer: PeerId(1),
peers: vec![PeerId(2), PeerId(3)]
}
);
assert_eq!(
serde_json::from_value::<ServerMessage>(json!({ "type": "peer-joined", "peer": 5 }))
.unwrap(),
ServerMessage::PeerJoined { peer: PeerId(5) }
);
assert_eq!(
serde_json::from_value::<ServerMessage>(json!({ "type": "peer-left", "peer": 5 }))
.unwrap(),
ServerMessage::PeerLeft { peer: PeerId(5) }
);
assert_eq!(
serde_json::from_value::<ServerMessage>(
json!({ "type": "relay", "from": 1, "payload": [1, 2] })
)
.unwrap(),
ServerMessage::Relay {
from: PeerId(1),
payload: json!([1, 2])
}
);
assert_eq!(
serde_json::from_value::<ServerMessage>(
json!({ "type": "error", "code": "permission_denied", "message": "no" })
)
.unwrap(),
ServerMessage::Error {
code: "permission_denied".into(),
message: "no".into()
}
);
}
#[test]
fn server_message_round_trips() {
for msg in [
ServerMessage::Welcome {
peer: PeerId(1),
peers: vec![],
},
ServerMessage::Offer {
from: PeerId(2),
sdp: "s".into(),
},
ServerMessage::Answer {
from: PeerId(2),
sdp: "s".into(),
},
ServerMessage::Ice {
from: PeerId(2),
candidate: "c".into(),
},
] {
let s = serde_json::to_string(&msg).unwrap();
assert_eq!(serde_json::from_str::<ServerMessage>(&s).unwrap(), msg);
}
}
}