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.
Update and overwrite vs synchronize
The User
interface offers two methods to update metadata:
-
Overwrite: Directly set and replace user fields like
name
,email
, andstatus
with new values on the server. -
Synchronize: Use the
updateAction
function for conditional updates in sync with the latest server data. This method adapts to server changes and may repeat until successful.
Choose based on whether you need to overwrite data or synchronize with server updates.
Method signature
These methods take the following parameters:
-
update()
and overwriteuser.update(
name: String?,
externalId: String?,
profileUrl: String?,
email: String?,
custom: CustomObject?,
status: String?,
type: String?,
): PNFuture<User> -
update()
and synchronizeuser.update(
updateAction: UpdatableValues.(
user: User
) -> Unit
): PNFuture<User> -
updateUser()
chat.updateUser(
id: String,
name: String?,
externalId: String?,
profileUrl: String?,
email: String?,
custom: CustomObject?,
status: String?,
type: String?,
): PNFuture<User>
Input
Parameter | Type | Required in update() (overwrite) | Required in update() (synchronize) | Required in updateUser() | Default | Description |
---|---|---|---|---|---|---|
id | String | No | No | Yes | n/a | Unique user identifier. |
name | String | No | No | No | n/a | Display name for the user (must not be empty or consist only of whitespace characters). |
externalId | String | No | No | No | n/a | User's identifier in an external system. You can use it to match id with a similar identifier from an external database. |
profileUrl | String | No | No | No | n/a | URL of the user's profile picture. |
email | String | No | No | No | n/a | User's email address. |
custom | ObjectCustom | No | No | No | n/a | 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 | String | No | No | No | n/a | 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 | String | No | No | No | n/a | 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 . |
updateAction | Lambda function | No | Yes | No | n/a | A lambda function with a receiver of type UpdatableValues . If new values are fetched from the server, the function may run more than once. This function takes a User object as its parameter. Inside this function, you can compute new values for the user's fields and assign them to the properties of the UpdatableValues receiver:
|
API limits
To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.
Output
Type | Description |
---|---|
PNFuture<User> | Returned object containing the updated user metadata. |
Basic usage
Change the link to the user's support_agent_15
LinkedIn profile to https://www.linkedin.com/mkelly_vp2
.
-
update()
and overwrite
show all 19 lines// reference the "chat" object and invoke the "getUser()" method
chat.getUser("support_agent_15").async { result ->
result.onSuccess { user ->
// handle success: update the "linkedInUrl" custom field
user.update(
custom = mapOf(
"linkedInUrl" to "https://www.linkedin.com/mkelly_vp2"
)
).async { updateResult ->
updateResult.onSuccess {
// handle success
}.onFailure {
// handle failure
}
} -
update()
and synchronize
show all 21 lines// reference the "chat" object and invoke the "getUser()" method
chat.getUser("support_agent_15").async { result ->
result.onSuccess { user ->
// handle success: update using an updateAction
user.update {
this.custom = buildMap {
user.custom?.let { putAll(it) }
put("linkedInUrl", "https://www.linkedin.com/mkelly_vp2")
}
}.async { updateResult ->
updateResult.onSuccess {
// handle success
}.onFailure {
// handle failure -
updateUser()
// reference the "chat" object and invoke the "updateUser()" method
chat.updateUser(
id = "support_agent_15",
custom = mapOf(
"linkedInUrl" to "https://www.linkedin.com/mkelly_vp2"
)
).async { result ->
result.onSuccess {
// handle success
}.onFailure {
// handle failure
}
}
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 accept a callback function as an argument. The Chat SDK invokes this callback 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
. These methods also return the unsubscribe
function you can invoke to stop receiving objects
events and unsubscribe from the channel.
Method signature
These methods take the following parameters:
-
streamUpdates()
user.streamUpdates(callback: (user: User?) -> Unit): AutoCloseable
-
streamUpdatesOn()
class User {
companion object {
fun streamUpdatesOn(
users: Collection<User>,
callback: (users: Collection<User>) -> Unit
): AutoCloseable
}
}
Input
Parameter | Type | Required in streamUpdates() | Required in streamUpdatesOn() | Default | Description |
---|---|---|---|---|---|
users | Collection<User> | No | Yes | n/a | A collection of User objects for which you want to get updates. |
callback | (user: User?) -> Unit | Yes | No | n/a | Function that takes a single User object. It defines the custom behavior to be executed when detecting user changes. |
callback | (users: Collection<User>) -> Unit | No | Yes | n/a | Function that takes a set of User objects. It defines the custom behavior to be executed when detecting user changes. |
Output
Type | Description |
---|---|
AutoCloseable | Interface that lets you stop receiving channel-related updates (objects events) by invoking the close() method. |
Basic usage
Get updates on support_agent_15
.
-
streamUpdates()
// fetch a user by their ID
val supportAgentUser = chat.getUser("support_agent_15")
// stream updates for the specified user
val autoCloseable = supportAgentUser.streamUpdates { updatedUser ->
if (updatedUser != null) {
println("Updated user: $updatedUser")
} else {
println("User update failed or user doesn't exist.")
}
}
Get updates on support_agent_15
and support-manager
.
-
streamUpdatesOn()
// fetch users by their IDs
val supportAgentUser = chat.getUser("support_agent_15")
val supportManagerUser = chat.getUser("support-manager")
// collect the users you want to stream updates for
val usersToMonitor = listOf(supportAgentUser, supportManagerUser)
// stream updates for the specified users
val autoCloseable = User.streamUpdatesOn(users = usersToMonitor) { updatedUsers ->
println("Updated users: $updatedUsers")
}
Other examples
Stop listening to updates on support_agent_15
.
-
streamUpdates()
show all 24 linesclass MyActivity : AppCompatActivity() {
private lateinit var autoCloseable: AutoCloseable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// fetch a user by their ID
val supportAgentUser = chat.getUser("support_agent_15")
// start streaming updates for the specified user
autoCloseable = supportAgentUser.streamUpdates { updatedUser ->
if (updatedUser != null) {
println("Updated user: $updatedUser")
} else {
println("User update failed or user doesn't exist.")
}
Stop listening to updates on support_agent_15
and support-manager
.
-
streamUpdatesOn()
show all 29 linesclass MyActivity : AppCompatActivity() {
private lateinit var autoCloseable: AutoCloseable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// fetch users by their IDs
val supportAgentUser = chat.getUser("support_agent_15")
val supportManagerUser = chat.getUser("support_manager")
val users = listOf(supportAgentUser, supportManagerUser)
// start streaming updates for the specified users
autoCloseable = User.streamUpdatesOn(users = users) { updatedUsers ->
updatedUsers.forEach { updatedUser ->
if (updatedUser != null) {