Manage user updates
Update user details and receive real-time update events.
Requires App Context
Enable App Context in the Admin Portal to store user data.
Update user details
Edit user metadata with update() or updateUser().
update()- call on aUserobject (no ID needed)updateUser()- call on aChatobject (requires user ID)
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 id with 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 custom property 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 status to mark users in your chat app as invited, active, or archived. |
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 type to group users by their roles in your app, such as moderator, player, or support-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 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. |
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
Receive updates when User objects are edited or removed:
streamUpdates()- monitors a single userstreamUpdatesOn()- monitors multiple users
Callback behavior
streamUpdates(): returns updatedUserobject (ornullif deleted)streamUpdatesOn(): returns complete list of monitored users on any change
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 User objects for which you want to get updates. |
callbackType: (user: User?) -> UnitDefault: n/a | Yes | No | Function that takes a single User object. 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 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. |
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 // The callback receives the entire updated User object each time a change occurs.
7 if (updatedUser != null) {
8 println("Updated user: $updatedUser")
9 } else {
10 println("User was deleted")
11 }
12}
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 // The callback receives the complete list of all users you're monitoring
11 // each time any change occurs.
12 println("Updated users: $updatedUsers")
13}
Other examples
Stop listening to updates on support_agent_15.
-
streamUpdates()
show all 25 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 // The callback receives the entire updated User object each time a change occurs.
12 if (updatedUser != null) {
13 println("Updated user: $updatedUser")
14 } else {
15 println("User was deleted")
Stop listening to updates on support_agent_15 and support-manager.
-
streamUpdatesOn()
show all 31 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 // The callback receives the complete list of all users you're monitoring
15 // each time any change occurs.