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, andstatuswith new values on the server.
- 
Synchronize: Use the updateActionfunction 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 overwrite1user.update(
 2 name: String?,
 3 externalId: String?,
 4 profileUrl: String?,
 5 email: String?,
 6 custom: CustomObject?,
 7 status: String?,
 8 type: String?,
 9): PNFuture<User>
- 
update()and synchronize1user.update(
 2 updateAction: UpdatableValues.(
 3 user: User
 4 ) -> Unit
 5): PNFuture<User>
- 
updateUser()1chat.updateUser(
 2 id: String,
 3 name: String?,
 4 externalId: String?,
 5 profileUrl: String?,
 6 email: String?,
 7 custom: CustomObject?,
 8 status: String?,
 9 type: String?,
 10): PNFuture<User>
Input
| Parameter | Required in update()(overwrite) | Required in update()(synchronize) | Required in updateUser() | Description | 
|---|---|---|---|---|
| idType:  StringDefault: n/a | No | No | Yes | Unique user identifier. | 
| nameType:  StringDefault: n/a | No | No | No | Display name for the user (must not be empty or consist only of whitespace characters). | 
| externalIdType:  StringDefault: n/a | No | No | No | User's identifier in an external system. You can use it to match idwith a similar identifier from an external database. | 
| profileUrlType:  StringDefault: n/a | No | No | No | URL of the user's profile picture. | 
| emailType:  StringDefault: n/a | No | No | No | User's email address. | 
| customType:  ObjectCustomDefault: n/a | No | 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 customproperty is not recommended in SDKs. | 
| statusType:  StringDefault: n/a | No | 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 statusto mark users in your chat app asinvited,active, orarchived. | 
| typeType:  StringDefault: n/a | No | 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 typeto group users by their roles in your app, such asmoderator,player, orsupport-agent. | 
| updateActionType: Lambda function Default: n/a | No | Yes | No | 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 Userobject as its parameter. Inside this function, you can compute new values for the user's fields and assign them to the properties of theUpdatableValuesreceiver:
 | 
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. | 
Sample code
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 lines1// reference the "chat" object and invoke the "getUser()" method
 2chat.getUser("support_agent_15").async { result ->
 3 result.onSuccess { user ->
 4 // handle success: update the "linkedInUrl" custom field
 5 user.update(
 6 custom = mapOf(
 7 "linkedInUrl" to "https://www.linkedin.com/mkelly_vp2"
 8 )
 9 ).async { updateResult ->
 10 updateResult.onSuccess {
 11 // handle success
 12 }.onFailure {
 13 // handle failure
 14 }
 15 }
- 
update()and synchronize
 show all 21 lines1// reference the "chat" object and invoke the "getUser()" method
 2chat.getUser("support_agent_15").async { result ->
 3 result.onSuccess { user ->
 4 // handle success: update using an updateAction
 5 user.update {
 6 this.custom = buildMap {
 7 user.custom?.let { putAll(it) }
 8 put("linkedInUrl", "https://www.linkedin.com/mkelly_vp2")
 9 }
 10
 11 }.async { updateResult ->
 12 updateResult.onSuccess {
 13 // handle success
 14 }.onFailure {
 15 // handle failure
- 
updateUser()1// reference the "chat" object and invoke the "updateUser()" method
 2chat.updateUser(
 3 id = "support_agent_15",
 4 custom = mapOf(
 5 "linkedInUrl" to "https://www.linkedin.com/mkelly_vp2"
 6 )
 7).async { result ->
 8 result.onSuccess {
 9 // handle success
 10 }.onFailure {
 11 // handle failure
 12 }
 13}
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 single- Userobject.
- streamUpdatesOn()checks updates on a list of- Userobjects.
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()1user.streamUpdates(callback: (user: User?) -> Unit): AutoCloseable
- 
streamUpdatesOn()1class User {
 2 companion object {
 3 fun streamUpdatesOn(
 4 users: Collection<User>,
 5 callback: (users: Collection<User>) -> Unit
 6 ): AutoCloseable
 7 }
 8}
Input
| Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description | 
|---|---|---|---|
| usersType:  Collection<User>Default: n/a | No | Yes | A collection of Userobjects for which you want to get updates. | 
| callbackType:  (user: User?) -> UnitDefault: n/a | Yes | No | Function that takes a single Userobject. It defines the custom behavior to be executed when detecting user changes. | 
| callbackType:  (users: Collection<User>) -> UnitDefault: n/a | No | Yes | Function that takes a set of Userobjects. 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 ( objectsevents) by invoking theclose()method. | 
Sample code
Get updates on support_agent_15.
- 
streamUpdates()1// fetch a user by their ID
 2val supportAgentUser = chat.getUser("support_agent_15")
 3
 4// stream updates for the specified user
 5val autoCloseable = supportAgentUser.streamUpdates { updatedUser ->
 6 if (updatedUser != null) {
 7 println("Updated user: $updatedUser")
 8 } else {
 9 println("User update failed or user doesn't exist.")
 10 }
 11}
Get updates on support_agent_15 and support-manager.
- 
streamUpdatesOn()1// fetch users by their IDs
 2val supportAgentUser = chat.getUser("support_agent_15")
 3val supportManagerUser = chat.getUser("support-manager")
 4
 5// collect the users you want to stream updates for
 6val usersToMonitor = listOf(supportAgentUser, supportManagerUser)
 7
 8// stream updates for the specified users
 9val autoCloseable = User.streamUpdatesOn(users = usersToMonitor) { updatedUsers ->
 10 println("Updated users: $updatedUsers")
 11}
Other examples
Stop listening to updates on support_agent_15.
- 
streamUpdates()
 show all 24 lines1class MyActivity : AppCompatActivity() {
 2 private lateinit var autoCloseable: AutoCloseable
 3
 4 override fun onCreate(savedInstanceState: Bundle?) {
 5 super.onCreate(savedInstanceState)
 6 // fetch a user by their ID
 7 val supportAgentUser = chat.getUser("support_agent_15")
 8
 9 // start streaming updates for the specified user
 10 autoCloseable = supportAgentUser.streamUpdates { updatedUser ->
 11 if (updatedUser != null) {
 12 println("Updated user: $updatedUser")
 13 } else {
 14 println("User update failed or user doesn't exist.")
 15 }
Stop listening to updates on support_agent_15 and support-manager.
- 
streamUpdatesOn()
 show all 29 lines1class MyActivity : AppCompatActivity() {
 2 private lateinit var autoCloseable: AutoCloseable
 3
 4 override fun onCreate(savedInstanceState: Bundle?) {
 5 super.onCreate(savedInstanceState)
 6 // fetch users by their IDs
 7 val supportAgentUser = chat.getUser("support_agent_15")
 8 val supportManagerUser = chat.getUser("support_manager")
 9
 10 val users = listOf(supportAgentUser, supportManagerUser)
 11
 12 // start streaming updates for the specified users
 13 autoCloseable = User.streamUpdatesOn(users = users) { updatedUsers ->
 14 updatedUsers.forEach { updatedUser ->
 15 if (updatedUser != null) {