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
{{ message }}
This repository was archived by the owner on Aug 8, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/Server Module Languages/C#/ModuleReference.md
+3-13Lines changed: 3 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -289,6 +289,9 @@ These are two special kinds of reducers that can be used to respond to module li
289
289
290
290
-`ReducerKind.Init` - this reducer will be invoked when the module is first published.
291
291
-`ReducerKind.Update` - this reducer will be invoked when the module is updated.
292
+
-`ReducerKind.Connect` - this reducer will be invoked when a client connects to the database.
293
+
-`ReducerKind.Disconnect` - this reducer will be invoked when a client disconnects from the database.
294
+
292
295
293
296
Example:
294
297
@@ -299,16 +302,3 @@ public static void Init()
299
302
Log("...and we're live!");
300
303
}
301
304
```
302
-
303
-
### Connection events
304
-
305
-
`OnConnect` and `OnDisconnect``SpacetimeDB.Runtime` events are triggered when a client connects or disconnects from the database. They can be used to initialize per-client state or to clean up after the client disconnects. They get passed an instance of the earlier mentioned `DbEventArgs` which can be used to distinguish clients via its `Sender` field.
Copy file name to clipboardExpand all lines: docs/Server Module Languages/C#/index.md
+39-41Lines changed: 39 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,64 +182,62 @@ You could extend the validation in `ValidateMessage` in similar ways to `Validat
182
182
183
183
## Set users' online status
184
184
185
-
In C# modules, you can register for OnConnect and OnDisconnect events in a special initializer function that uses the attribute `ModuleInitializer`. We'll use the `OnConnect` event to create a `User` record for the client if it doesn't yet exist, and to set its online status.
185
+
In C# modules, you can register for `Connect` and `Disconnect` events by using a special `ReducerKind`. We'll use the `Connect` event to create a `User` record for the client if it doesn't yet exist, and to set its online status.
186
186
187
187
We'll use `User.FilterByIdentity` to look up a `User` row for `dbEvent.Sender`, if one exists. If we find one, we'll use `User.UpdateByIdentity` to overwrite it with a row that has `Online: true`. If not, we'll use `User.Insert` to insert a new row for our new user. All three of these methods are generated by the `[SpacetimeDB.Table]` attribute, with rows and behavior based on the row attributes. `FilterByIdentity` returns a nullable `User`, because the unique constraint from the `[SpacetimeDB.Column(ColumnAttrs.PrimaryKey)]` attribute means there will be either zero or one matching rows. `Insert` will throw an exception if the insert violates this constraint; if we want to overwrite a `User` row, we need to do so explicitly using `UpdateByIdentity`.
188
188
189
189
In `server/Lib.cs`, add the definition of the connect reducer to the `Module` class:
190
190
191
191
```C#
192
-
[ModuleInitializer]
193
-
publicstaticvoidInit()
192
+
[SpacetimeDB.Reducer(ReducerKind.Connect)]
193
+
publicstaticvoidOnConnect(DbEventArgsdbEventArgs)
194
194
{
195
-
OnConnect+= (dbEventArgs) =>
196
-
{
197
-
Log($"Connect {dbEventArgs.Sender}");
198
-
varuser=User.FindByIdentity(dbEventArgs.Sender);
195
+
Log($"Connect {dbEventArgs.Sender}");
196
+
varuser=User.FindByIdentity(dbEventArgs.Sender);
199
197
200
-
if (userisnotnull)
201
-
{
202
-
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
203
-
// set `Online: true`, but leave `Name` and `Identity` unchanged.
204
-
user.Online=true;
205
-
User.UpdateByIdentity(dbEventArgs.Sender, user);
206
-
}
207
-
else
198
+
if (userisnotnull)
199
+
{
200
+
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
201
+
// set `Online: true`, but leave `Name` and `Identity` unchanged.
202
+
user.Online=true;
203
+
User.UpdateByIdentity(dbEventArgs.Sender, user);
204
+
}
205
+
else
206
+
{
207
+
// If this is a new user, create a `User` object for the `Identity`,
208
+
// which is online, but hasn't set a name.
209
+
newUser
208
210
{
209
-
// If this is a new user, create a `User` object for the `Identity`,
210
-
// which is online, but hasn't set a name.
211
-
newUser
212
-
{
213
-
Name=null,
214
-
Identity=dbEventArgs.Sender,
215
-
Online=true,
216
-
}.Insert();
217
-
}
218
-
};
211
+
Name=null,
212
+
Identity=dbEventArgs.Sender,
213
+
Online=true,
214
+
}.Insert();
215
+
}
219
216
}
220
217
```
221
218
222
-
Similarly, whenever a client disconnects, the module will execute the `OnDisconnect` event if it's registered. We'll use it to un-set the `Online` status of the `User` for the disconnected client.
219
+
Similarly, whenever a client disconnects, the module will execute the `OnDisconnect` event if it's registered with `ReducerKind.Disconnect`. We'll use it to un-set the `Online` status of the `User` for the disconnected client.
223
220
224
221
Add the following code after the `OnConnect` lambda:
0 commit comments