Manage user updates
Update user details and receive events whenever someone updates them.
Requires App Context
To store data about users, you must enable App Context for your app's keyset in the Admin Portal.
Update user details
You can edit the metadata of an existing user with update()
and updateUser()
.
Both of these methods give the same output. The only difference is that you call a given method either on the Chat
(updateUser()
) or the User
(update()
) object. Depending on the object, these methods take a different set of input parameters - you either have to specify the user ID you want to update or not because it's already known.
Method signature
These methods take the following parameters:
-
update()
user.update(
name: String? = nil,
externalId: String? = nil,
profileUrl: String? = nil,
email: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
status: String? = nil,
type: String? = nil
) async throws -> UserImpl -
updateUser()
chat.updateUser(
id: String,
name: String? = nil,
externalId: String? = nil,
profileUrl: String? = nil,
email: String? = nil,
custom: [String: JSONCodableScalar]? = nil,
status: String? = nil,
type: String? = nil
) async throws -> UserImpl
Input
Parameter | Required in update() | Required in updateUser() | Description |
---|---|---|---|
id Type: String Default: n/a | No | Yes | Unique user identifier. |
name Type: String Default: n/a | No | No | Display name for the user (must not be empty or consist only of whitespace characters). |
externalId Type: String Default: n/a | No | No | User's identifier in an external system. You can use it to match id with a similar identifier from an external database. |
profileUrl Type: String Default: n/a | No | No | URL of the user's profile picture. |
email Type: String Default: n/a | No | No | User's email address. |
custom Type: [String: JSONCondableScalar] Default: n/a | No | No | JSON providing custom data about the user. Values must be scalar only; arrays or objects are not supported. Filtering App Context data through the custom property is not recommended in SDKs. |
status Type: String Default: n/a | No | No | Tag that lets you categorize your app users by their current state. The tag choice is entirely up to you and depends on your use case. For example, you can use status to mark users in your chat app as invited , active , or archived . |
type Type: String Default: n/a | No | No | Tag that lets you categorize your app users by their functional roles. The tag choice is entirely up to you and depends on your use case. For example, you can use type to group users by their roles in your app, such as moderator , player , or support-agent . |
API limits
To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.
Output
Parameter | Description |
---|---|
UserImpl | Returned object containing the updated user metadata. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Change the link to the user's support_agent_15
LinkedIn profile to https://www.linkedin.com/mkelly_vp2
.
-
update()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let user = try await chat.getUser(userId: "support_agent_15") {
let updatedUser = try await user.update(profileUrl: "https://www.linkedin.com/mkelly_vp2")
debugPrint("User profile updated on User object: \(updatedUser)")
} else {
debugPrint("User not found")
}
} -
updateUser()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
let updatedUser = try await chat.updateUser(
id: "support_agent_15",
profileUrl: "https://www.linkedin.com/mkelly_vp2"
)
debugPrint("Updated user object: \(updatedUser)")
}
Get user updates
Two methods let you receive updates about users (user IDs) added, edited, or removed on other clients:
streamUpdates()
checks updates on a singleUser
object.streamUpdatesOn()
checks updates on a list ofUser
objects.
Both methods return an async stream which is invoked whenever someone adds, changes, or removes user metadata.
Underneath, these methods subscribe the current user to a channel and add an objects event listener to receive all objects
(known as App Context) events of type uuid
.
Method signature
These methods take the following parameters:
-
streamUpdates()
user.streamUpdates() -> AsyncStream<UserImpl?>
-
streamUpdatesOn()
(static)UserImpl.streamUpdatesOn(users: [UserImpl]) -> AsyncStream<[UserImpl]>
Input
Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description |
---|---|---|---|
users Type: [UserImpl] Default: n/a | No | Yes | A collection of UserImpl objects for which you want to get updates. |
Output
An asynchronous stream that produces updates when the underlying user(s) change.
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get updates on support_agent_15
.
-
streamUpdates()
- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let user = try await chat.getUser(userId: "support_agent_15") {
for await updatedUser in user.streamUpdates() {
debugPrint("Updated user object: \(String(describing: updatedUser))")
}
} else {
debugPrint("User not found")
}
}// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive new updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a reference of type "UserImpl" named "support_agent_15" representing
// previously fetched user object with the `support_agent_15` identifier:
closeable = support_agent_15.streamUpdates { updatedUser in
if let updatedUser = updatedUser {
debugPrint("User updated: \(updatedUser)")
} else {
debugPrint("User was removed.")
}
}
Get updates on support_agent_15
and support_manager
.
-
streamUpdatesOn()
- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let firstUser = try await chat.getUser(userId: "support_agent_15"), let secondUser = try await chat.getUser(userId: "support_manager") {
for await updatedUser in UserImpl.streamUpdatesOn(users: [firstUser, secondUser]) {
debugPrint("Updated user object: \(String(describing: updatedUser))")
}
} else {
debugPrint("User not found")
}
}// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive new updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a "chat" instance available and a "UserImpl" representation
// of "support_agent_15", and "support_manager"
closeable = UserImpl.streamUpdatesOn(users: [support_agent_15, support_manager]) { updatedUsers in
debugPrint("Users updated: \(updatedUsers.map { $0.id })")
}