From f02f8f47a8d7f0b3a3f38c8810f2449dd72a1a8e Mon Sep 17 00:00:00 2001 From: rekhoff Date: Wed, 4 Jun 2025 08:40:08 -0700 Subject: [PATCH 1/7] Added How To Reject Client Connections guide --- docs/how-to/reject-client-connections.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 docs/how-to/reject-client-connections.md diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md new file mode 100644 index 00000000..875eec7f --- /dev/null +++ b/docs/how-to/reject-client-connections.md @@ -0,0 +1,30 @@ +# Rejecting Client Connections + +SpacetimeDB provides a way to disconnect a client during a client connection attempt. +:::server-rust +In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client would be disconnected. + +Here is a simple example where the server module throws an error for all incoming client connections. +```rust +#[reducer(client_connected)] +// Called when a client connects to a SpacetimeDB database server +pub fn client_connected(ctx: &ReducerContext) { + return Err("All incoming client connections are being rejected. Normally you'd want to only throw an error after your validation logic indicates the client is not authorized.".into()); +} +``` +::: +:::server-csharp +In C#, if we throw an exception during the `ClientConnected` reducer, the client would be disconnected. + +Here is a simple example where the server module throws an error for all incoming client connections. +```csharp +[Reducer(ReducerKind.ClientConnected)] +// Called when a client connects to a SpacetimeDB database server +public static void ClientConnected(ReducerContext ctx) +{ + throw new Exception("All incoming client connections are being rejected. Normally you'd want to only throw an error after your validation logic indicates the client is not authorized."); +} +``` +::: + +From the client's perspective, this disconnection behavior is currently undefined, but they will be disconnected from the server's perspective. \ No newline at end of file From 110e4d085480545adccfc487df539c9dc3818334 Mon Sep 17 00:00:00 2001 From: rekhoff Date: Thu, 5 Jun 2025 17:23:30 -0700 Subject: [PATCH 2/7] Corrected rejecting client connection code and behavior descriptions --- docs/how-to/reject-client-connections.md | 34 +++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md index 875eec7f..7551db8d 100644 --- a/docs/how-to/reject-client-connections.md +++ b/docs/how-to/reject-client-connections.md @@ -1,17 +1,31 @@ # Rejecting Client Connections SpacetimeDB provides a way to disconnect a client during a client connection attempt. + :::server-rust -In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client would be disconnected. +In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client will be disconnected. Here is a simple example where the server module throws an error for all incoming client connections. ```rust #[reducer(client_connected)] -// Called when a client connects to a SpacetimeDB database server -pub fn client_connected(ctx: &ReducerContext) { - return Err("All incoming client connections are being rejected. Normally you'd want to only throw an error after your validation logic indicates the client is not authorized.".into()); +pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { + let client_is_rejected = true; + if client_is_rejected { + Err("The client connection was rejected. With our current code logic, all clients will be rejected.".to_string()) + } else { + Ok(()) + } } ``` + +From a rust client's perspective, this disconnection behavior is currently undefined and will cause a panic reading: +`Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` + +From a C# client's perspective, this disconnection behavior is currently undefined and will cause an error reading: +`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + +Regardless of the client type, from the rust server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: +`ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` ::: :::server-csharp In C#, if we throw an exception during the `ClientConnected` reducer, the client would be disconnected. @@ -22,9 +36,15 @@ Here is a simple example where the server module throws an error for all incomin // Called when a client connects to a SpacetimeDB database server public static void ClientConnected(ReducerContext ctx) { - throw new Exception("All incoming client connections are being rejected. Normally you'd want to only throw an error after your validation logic indicates the client is not authorized."); + throw new Exception("The client connection was rejected. With our current code logic, all clients will be rejected."); } ``` -::: -From the client's perspective, this disconnection behavior is currently undefined, but they will be disconnected from the server's perspective. \ No newline at end of file +From a C# client's perspective, this disconnection behavior is currently undefined and will cause an error reading: +`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + +From a Rust client's perspective, they will receive an `on_disconnected` event with no error message. + +Regardless of the client type, from the C# server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: +`ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.` +::: From 8ffdda181b3d98c7cc6ed7ad06027cee3c5d4137 Mon Sep 17 00:00:00 2001 From: rekhoff Date: Tue, 10 Jun 2025 06:05:54 -0700 Subject: [PATCH 3/7] Added TypeScript behavior of rejected clients. --- docs/how-to/reject-client-connections.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md index 7551db8d..0f212e9f 100644 --- a/docs/how-to/reject-client-connections.md +++ b/docs/how-to/reject-client-connections.md @@ -18,11 +18,14 @@ pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { } ``` -From a rust client's perspective, this disconnection behavior is currently undefined and will cause a panic reading: -`Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` +Client behavior can vary by client type. For example: +* **C# clients**: Client disconnection behavior is currently undefined and will cause a panic reading: + `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` -From a C# client's perspective, this disconnection behavior is currently undefined and will cause an error reading: -`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` +* **Rust clients**: Client disconnection behavior is currently undefined and will cause an error reading: + `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. Regardless of the client type, from the rust server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: `ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` @@ -40,10 +43,13 @@ public static void ClientConnected(ReducerContext ctx) } ``` -From a C# client's perspective, this disconnection behavior is currently undefined and will cause an error reading: +Client behavior can vary by client type. For example: +* **C# clients**: Client disconnection behavior is currently undefined and will cause an error reading: `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` -From a Rust client's perspective, they will receive an `on_disconnected` event with no error message. +* **Rust clients**: Client will receive an `on_disconnected` event with no error message. + +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. Regardless of the client type, from the C# server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: `ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.` From 29b426c4f24f601d47103fef8ab966d3efe02b11 Mon Sep 17 00:00:00 2001 From: rekhoff Date: Tue, 10 Jun 2025 09:11:04 -0700 Subject: [PATCH 4/7] Implement suggested change. Co-authored-by: Phoebe Goldman --- docs/how-to/reject-client-connections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md index 0f212e9f..c97a265d 100644 --- a/docs/how-to/reject-client-connections.md +++ b/docs/how-to/reject-client-connections.md @@ -31,7 +31,7 @@ Regardless of the client type, from the rust server's perspective, the client wi `ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` ::: :::server-csharp -In C#, if we throw an exception during the `ClientConnected` reducer, the client would be disconnected. +In C#, if we throw an exception during the `ClientConnected` reducer, the client will be disconnected. Here is a simple example where the server module throws an error for all incoming client connections. ```csharp From 6930cd0f3c255764c5efb22f8b73e13ac3cbec9b Mon Sep 17 00:00:00 2001 From: rekhoff Date: Tue, 10 Jun 2025 09:14:48 -0700 Subject: [PATCH 5/7] Corrected some terminology --- docs/how-to/reject-client-connections.md | 6 +-- docs/how-to/reject-client-connections.md~ | 56 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 docs/how-to/reject-client-connections.md~ diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md index 0f212e9f..c41fc598 100644 --- a/docs/how-to/reject-client-connections.md +++ b/docs/how-to/reject-client-connections.md @@ -19,10 +19,10 @@ pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { ``` Client behavior can vary by client type. For example: -* **C# clients**: Client disconnection behavior is currently undefined and will cause a panic reading: +* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` -* **Rust clients**: Client disconnection behavior is currently undefined and will cause an error reading: +* **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading: `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` * **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. @@ -44,7 +44,7 @@ public static void ClientConnected(ReducerContext ctx) ``` Client behavior can vary by client type. For example: -* **C# clients**: Client disconnection behavior is currently undefined and will cause an error reading: +* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` * **Rust clients**: Client will receive an `on_disconnected` event with no error message. diff --git a/docs/how-to/reject-client-connections.md~ b/docs/how-to/reject-client-connections.md~ new file mode 100644 index 00000000..af5413ec --- /dev/null +++ b/docs/how-to/reject-client-connections.md~ @@ -0,0 +1,56 @@ +# Rejecting Client Connections + +SpacetimeDB provides a way to disconnect a client during a client connection attempt. + +:::server-rust +In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client will be disconnected. + +Here is a simple example where the server module throws an error for all incoming client connections. +```rust +#[reducer(client_connected)] +pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { + let client_is_rejected = true; + if client_is_rejected { + Err("The client connection was rejected. With our current code logic, all clients will be rejected.".to_string()) + } else { + Ok(()) + } +} +``` + +Client behavior can vary by client type. For example: +* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: + `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` + +* **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading: + `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. + +Regardless of the client type, from the rust server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: +`ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` +::: +:::server-csharp +In C#, if we throw an exception during the `ClientConnected` reducer, the client would be disconnected. + +Here is a simple example where the server module throws an error for all incoming client connections. +```csharp +[Reducer(ReducerKind.ClientConnected)] +// Called when a client connects to a SpacetimeDB database server +public static void ClientConnected(ReducerContext ctx) +{ + throw new Exception("The client connection was rejected. With our current code logic, all clients will be rejected."); +} +``` + +Client behavior can vary by client type. For example: +* **C# clients**: Client disconnection behavior is currently undefined and will cause an error reading: +`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + +* **Rust clients**: Client will receive an `on_disconnected` event with no error message. + +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. + +Regardless of the client type, from the C# server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: +`ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.` +::: From e93783316f9dd66bbeeb9982f34f56213c6d5eb2 Mon Sep 17 00:00:00 2001 From: rekhoff Date: Wed, 11 Jun 2025 08:12:50 -0700 Subject: [PATCH 6/7] Corrected behavior description of Rust clients disconnects --- docs/how-to/reject-client-connections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how-to/reject-client-connections.md b/docs/how-to/reject-client-connections.md index 1ab38068..5ca1d073 100644 --- a/docs/how-to/reject-client-connections.md +++ b/docs/how-to/reject-client-connections.md @@ -23,7 +23,7 @@ Client behavior can vary by client type. For example: `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` * **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading: - `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` + `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` * **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. From d1f0b4b7931d13fa809a9ae293ce62d62455f93b Mon Sep 17 00:00:00 2001 From: rekhoff Date: Wed, 11 Jun 2025 08:18:32 -0700 Subject: [PATCH 7/7] Removed tilde file --- docs/how-to/reject-client-connections.md~ | 56 ----------------------- 1 file changed, 56 deletions(-) delete mode 100644 docs/how-to/reject-client-connections.md~ diff --git a/docs/how-to/reject-client-connections.md~ b/docs/how-to/reject-client-connections.md~ deleted file mode 100644 index af5413ec..00000000 --- a/docs/how-to/reject-client-connections.md~ +++ /dev/null @@ -1,56 +0,0 @@ -# Rejecting Client Connections - -SpacetimeDB provides a way to disconnect a client during a client connection attempt. - -:::server-rust -In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client will be disconnected. - -Here is a simple example where the server module throws an error for all incoming client connections. -```rust -#[reducer(client_connected)] -pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { - let client_is_rejected = true; - if client_is_rejected { - Err("The client connection was rejected. With our current code logic, all clients will be rejected.".to_string()) - } else { - Ok(()) - } -} -``` - -Client behavior can vary by client type. For example: -* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: - `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` - -* **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading: - `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` - -* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. - -Regardless of the client type, from the rust server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: -`ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` -::: -:::server-csharp -In C#, if we throw an exception during the `ClientConnected` reducer, the client would be disconnected. - -Here is a simple example where the server module throws an error for all incoming client connections. -```csharp -[Reducer(ReducerKind.ClientConnected)] -// Called when a client connects to a SpacetimeDB database server -public static void ClientConnected(ReducerContext ctx) -{ - throw new Exception("The client connection was rejected. With our current code logic, all clients will be rejected."); -} -``` - -Client behavior can vary by client type. For example: -* **C# clients**: Client disconnection behavior is currently undefined and will cause an error reading: -`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` - -* **Rust clients**: Client will receive an `on_disconnected` event with no error message. - -* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. - -Regardless of the client type, from the C# server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: -`ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.` -:::