You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `onBeforeConnect` hook is called whenever a new client connects to the actor. Can be async. Clients can pass parameters when connecting, accessible via `params`. This hook is used for connection validation and can throw errors to reject connections.
163
+
164
+
The `onBeforeConnect` hook does NOT return connection state - it's used solely for validation.
165
+
166
+
```typescript
167
+
import { actor } from"rivetkit";
168
+
169
+
const chatRoom =actor({
170
+
state: { messages: [] },
171
+
172
+
// Method 1: Use a static default connection state
// Authentication is valid, connection will proceed
196
+
// The actual connection state will come from connState or createConnState
197
+
},
198
+
199
+
actions: { /* ... */ }
200
+
});
201
+
```
137
202
138
-
The connection lifecycle has several hooks:
203
+
Connections cannot interact with the actor until this method completes successfully. Throwing an error will abort the connection. This can be used for authentication, see [Authentication](/docs/actors/authentication) for details.
139
204
140
-
-`onBeforeConnect`: Called before a client connects, returns the connection state
141
-
-`onConnect`: Called when a client successfully connects
142
-
-`createState`: Called when creating a connection state
143
-
-`onDisconnect`: Called when a client disconnects
205
+
### `onConnect`
144
206
145
-
See the documentation on [Actor Lifecycle](/docs/actors/lifecycle) for more details.
Called when a client disconnects from the actor. Can be async. Receives the connection object as a second parameter. Use this to clean up any connection-specific resources.
0 commit comments