This repository was archived by the owner on Aug 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Added How To Reject Client Connections guide #352
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f02f8f4
Added How To Reject Client Connections guide
rekhoff 110e4d0
Corrected rejecting client connection code and behavior descriptions
rekhoff 8ffdda1
Added TypeScript behavior of rejected clients.
rekhoff 29b426c
Implement suggested change.
rekhoff 6930cd0
Corrected some terminology
rekhoff 520d2b3
Merge branch 'rekhoff/how-to-reject-client-connections' of https://gi…
rekhoff e937833
Corrected behavior description of Rust clients disconnects
rekhoff d1f0b4b
Removed tilde file
rekhoff 7fe2d8b
Corrected wrong error message for C# Clients on rejected client conne…
rekhoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
`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. | ||
|
||
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 will 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 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. | ||
|
||
* **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.` | ||
::: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.