The Stream user object is central to the chat system and appears in many API responses, effectively following the user throughout the platform. Only an id is required to create a user but you can store additional custom data. We recommend only storing what is necessary for Chat such as a username and image URL.
The connectUser method automatically creates and updates the user. If you are looking to onboard your userbase lazily, this is typically a perfectly viable option.
However, it is also common to add your users to Stream before going Live and keep properties of your user base in sync with Stream. For this you'll want to use the upsertUsers function server-side and send users in bulk.
The upsertUser method creates or updates a user, replacing its existing data with the new payload (see below for partial updates). To create or update users in batches of up to 100, use the upsertUsers or partialUpdateUsers APIs, which accept an array of user objects.
Depending on the permission configuration of your application, you may also allow users to update their own user objects client-side.
// Only Id field is required, the rest is optionalvar createOrUpdateUser = new StreamUserUpsertRequest{ Id = "my-user-id", // BanExpires = DateTimeOffset.Now.AddDays(7), // Banned = true, // Invisible = true, // Role = "user", // Name = "David", // Image = "image-url", // You can upload image to Stream CDN or your own // CustomData = new StreamCustomDataRequest //{ // { "Age", 24 }, // { "Passions", new string[] { "Tennis", "Football", "Basketball" } } //}};// Upsert means: update user with a given ID or create a new one if it doesn't existvar users = await Client.UpsertUsers(new[] { createOrUpdateUser });
And for a batch of users, simply add additional entries (up to 100) into the array you pass to upsertUsers :
var usersToCreateOrUpdate = new[]{ new StreamUserUpsertRequest { Id = "my-user-id", Role = "user", }, new StreamUserUpsertRequest { Id = "my-user-id-2", // BanExpires = DateTimeOffset.Now.AddDays(7), // Banned = true, // Invisible = true, // Role = "user", // Name = "David", // Image = "image-url", // You can upload image to Stream CDN or your own // CustomData = new StreamCustomDataRequest //{ // { "Age", 24 }, // { "Passions", new string[] { "Tennis", "Football", "Basketball" } } //} },};// Upsert means: update user with a given ID or create a new one if it doesn't existvar users = await Client.UpsertUsers(usersToCreateOrUpdate);
If any user in a batch of users contains an error, the entire batch will fail, and the first error encountered will be returned.
If you need to update a subset of properties for a user(s), you can use a partial update method. Both set and unset parameters can be provided to add, modify, or remove attributes to or from the target user(s). The set and unset parameters can be used separately or combined.
Clients can set a username, by setting the name custom field. The field is optional and by default has no uniqueness constraints applied to it, however this is configurable by setting the enforce_unique_username to either app or team.
When checking for uniqueness, the name is normalized, by removing any white-space or other special characters, and finally transforming it to lowercase. So "John Doe" is considered a duplicate of "john doe", "john.doe", etc.
With the setting at app, creating or updating a user fails if the username already exists anywhere in the app. With team, it only fails if the username exists within the same team.
// Enable uniqueness constraints on App levelawait client.updateAppSettings({ enforce_unique_usernames: "app",});// Enable uniqueness constraints on Team levelawait client.updateAppSettings({ enforce_unique_usernames: "team",});
Models = GetStream::Generated::Models# Enable uniqueness constraints on App levelclient.common.update_app( Models::UpdateAppRequest.new(enforce_unique_usernames: 'app'))# Enable uniqueness constraints on Team levelclient.common.update_app( Models::UpdateAppRequest.new(enforce_unique_usernames: 'team'))
# Enable uniqueness constraints on App levelclient.update_app(enforce_unique_usernames="app")# Enable uniqueness constraints on Team levelclient.update_app(enforce_unique_usernames="team")
// Enable uniqueness constraints on App levelclient.updateApp(UpdateAppRequest.builder() .enforceUniqueUsernames("app") .build()).execute();// Enable uniqueness constraints on Team levelclient.updateApp(UpdateAppRequest.builder() .enforceUniqueUsernames("team") .build()).execute();
// dotnet add package getstream-netusing GetStream;using GetStream.Models;var client = new StreamClient("{{ api_key }}", "{{ api_secret }}");// Enable uniqueness constraints on App levelawait client.UpdateAppAsync(new UpdateAppRequest{ EnforceUniqueUsernames = "app"});// Enable uniqueness constraints on Team levelawait client.UpdateAppAsync(new UpdateAppRequest{ EnforceUniqueUsernames = "team"});
// Enable uniqueness constraints on App levelresp, err := client.UpdateApp(ctx, &getstream.UpdateAppRequest{ EnforceUniqueUsernames: getstream.PtrTo("app"),})// Enable uniqueness constraints on Team levelresp, err = client.UpdateApp(ctx, &getstream.UpdateAppRequest{ EnforceUniqueUsernames: getstream.PtrTo("team"),})
use GetStream\GeneratedModels as Models;// Enable uniqueness constraints on App level$client->updateApp(new Models\UpdateAppRequest( enforceUniqueUsernames: "app",));// Enable uniqueness constraints on Team level$client->updateApp(new Models\UpdateAppRequest( enforceUniqueUsernames: "team",));
// This can be set in https://dashboard.getstream.io/ -> Open your application -> Overview -> Authentication
Enabling this setting will only enforce the constraint going forward and will not try to validate existing usernames.
To deactivate a user, Stream Chat exposes a server-side deactivateUser method. A deactivated user cannot connect to Stream Chat but will be present in user queries and channel history.
use GetStream\GeneratedModels as Models;$response = $client->deactivateUser($userId, new Models\DeactivateUserRequest());$response = $client->deactivateUser($userId, new Models\DeactivateUserRequest( markMessagesDeleted: true, createdByID: "joe",));
Many users (up to 100) can be deactivated and reactivated with a single call. The operation runs asynchronously, and the response contains a task_id which can be polled using the getTask endpoint to check the status of the operation.
let resp = await serverClient.deactivateUsers([userIDs, ...], { created_by_id: userID, // optional: define who deactivated the users mark_messages_deleted: true // optional: the messages will be marked as deleted})
// This is a server-side only feature, choose any of our server-side SDKs to use it
use GetStream\GeneratedModels as Models;$response = $client->reactivateUser($userId, new Models\ReactivateUserRequest());$response = $client->reactivateUser($userId, new Models\ReactivateUserRequest( restoreMessages: true, name: "I am back", createdByID: "joe",));
// dotnet add package getstream-netusing GetStream;using GetStream.Models;var client = new StreamClient("{{ api_key }}", "{{ api_secret }}");await client.ReactivateUserAsync(userId, new ReactivateUserRequest());// With options:await client.ReactivateUserAsync(userId, new ReactivateUserRequest{ RestoreMessages = true, Name = "I am back", CreatedByID = "joe"});
You can delete up to 100 users and optionally all of their channels and messages using this method. First the users are marked deleted synchronously so the user will not be directly visible in the API. Then the process deletes the user and related objects asynchronously.
// This is a server-side only feature, choose any of our server-side SDKs to use it
The deleteUsers method is an asynchronous API where the response contains a task_id which can be polled using the getTask endpoint to check the status of the deletions.
These are the request parameters which determine what user data is deleted:
name
type
description
default
optional
user_ids
array
List of users who will be deleted
-
user
enum (soft, pruning, hard)
Soft: marks user as deleted and retains all user data. Pruning: marks user as deleted and nullifies user information. Hard: deletes user completely - this requires hard option for messages and conversation as well.
-
✓
conversations
enum (soft, hard)
Soft: marks all conversation channels as deleted (same effect as Delete Channels with 'hard' option disabled). Hard: deletes channel and all its data completely including messages (same effect as Delete Channels with 'hard' option enabled).
✓
messages
enum (soft, pruning, hard)
Soft: marks all user messages as deleted without removing any related message data. Pruning: marks all user messages as deleted, nullifies message information and removes some message data such as reactions and flags. Hard: deletes messages completely with all related information.
-
✓
new_channel_owner_id
string
Channels owned by hard-deleted users will be transferred to this userID.
-
✓
When deleting a user, if you wish to transfer ownership of their channels to another user, provide that user's ID in the new_channel_owner_id field. Otherwise, the channel owner will be updated to a system generated ID like delete-user-8219f6578a7395g
If users are soft deleted, they can be restored using the server-side client. However, only the user's metadata is restored; memberships, messages, reactions, etc. are not restored.
The Query Users method lets you search for users, though in many cases it's more practical to query your own user database instead. Like other Stream query APIs, it accepts filter, sort, and options parameters.
val request = QueryUsersRequest( filter = Filters.`in`("id", listOf("john", "jack", "jessie")), querySort = QuerySortByField.descByName("last_active"), offset = 0, limit = 10,)client.queryUsers(request).enqueue { result -> if (result is Result.Success) { val users: List<User> = result.value } else { // Handle Result.Failure }}
All filters use a Mongoose-style syntax; however, we do not run MongoDB on the backend, so only a subset of Mongoose queries are supported. The supported filters are described below.
var filters = new IFieldFilterRule[]{ UserFilter.Name.Autocomplete("Ro")};// Returns collection of IStreamUservar users = await Client.QueryUsersAsync(filters);
You can use the last_active field with the $exists operator to find users who have never connected. Use $exists: false for users who have never been active, or $exists: true for users who have connected at least once.