Skip to content

Added How To Reject Client Connections guide #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/how-to/reject-client-connections.md
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.`
:::