Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.

Commit 38ab427

Browse files
rekhoffbfops
andauthored
Adding reject-client-connections doc. (#2973)
# Description of Changes This originally was a PR in the Docs repo: #352 This is the work portion of clockworklabs/SpacetimeDBPrivate#1700 This adds a "How To Reject Client Connections" guide in both Rust and C# using the language toggle functionality. Testing: - [X] Validated code behavior presented in guide resulted in described behavior. --------- Co-authored-by: Zeke Foppa <[email protected]> Co-authored-by: Zeke Foppa <[email protected]>
1 parent ef24fcc commit 38ab427

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Rejecting Client Connections
2+
3+
SpacetimeDB provides a way to disconnect a client during a client connection attempt.
4+
5+
:::server-rust
6+
In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client will be disconnected.
7+
8+
Here is a simple example where the server module throws an error for all incoming client connections.
9+
```rust
10+
#[reducer(client_connected)]
11+
pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> {
12+
let client_is_rejected = true;
13+
if client_is_rejected {
14+
Err("The client connection was rejected. With our current code logic, all clients will be rejected.".to_string())
15+
} else {
16+
Ok(())
17+
}
18+
}
19+
```
20+
21+
Client behavior can vary by client type. For example:
22+
* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading:
23+
`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.`
24+
25+
* **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading:
26+
`Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }`
27+
28+
* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006.
29+
30+
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:
31+
`ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.`
32+
:::
33+
:::server-csharp
34+
In C#, if we throw an exception during the `ClientConnected` reducer, the client will be disconnected.
35+
36+
Here is a simple example where the server module throws an error for all incoming client connections.
37+
```csharp
38+
[Reducer(ReducerKind.ClientConnected)]
39+
// Called when a client connects to a SpacetimeDB database server
40+
public static void ClientConnected(ReducerContext ctx)
41+
{
42+
throw new Exception("The client connection was rejected. With our current code logic, all clients will be rejected.");
43+
}
44+
```
45+
46+
Client behavior can vary by client type. For example:
47+
* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading:
48+
`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.`
49+
50+
* **Rust clients**: Client will receive an `on_disconnected` event with no error message.
51+
52+
* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006.
53+
54+
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:
55+
`ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.`
56+
:::

docs/nav.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const nav = {
4444
page('Row Level Security', 'rls', 'rls/index.md'),
4545
section('How To'),
4646
page('Incremental Migrations', 'how-to/incremental-migrations', 'how-to/incremental-migrations.md'),
47+
page('Reject Client Connections', 'how-to/reject-client-connections', 'how-to/reject-client-connections.md'),
4748
section('HTTP API'),
4849
page('Authorization', 'http/authorization', 'http/authorization.md'),
4950
page('`/identity`', 'http/identity', 'http/identity.md'),

nav.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const nav: Nav = {
9696

9797
section('How To'),
9898
page('Incremental Migrations', 'how-to/incremental-migrations', 'how-to/incremental-migrations.md'),
99+
page('Reject Client Connections', 'how-to/reject-client-connections', 'how-to/reject-client-connections.md'),
99100

100101
section('HTTP API'),
101102
page('Authorization', 'http/authorization', 'http/authorization.md'),

0 commit comments

Comments
 (0)