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?,
externalId: String?,
profileUrl: String?,
email: String?,
custom: CustomObject?,
status: String?,
type: String?,
): 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() | Required in updateUser() | Default | Description |
---|---|---|---|---|---|
id | String | No | Yes | n/a | Unique user identifier. |
name | String | No | No | n/a | Display name for the user (must not be empty or consist only of whitespace characters). |
externalId | String | 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 | n/a | URL of the user's profile picture. |
email | String | No | No | n/a | User's email address. |
custom | ObjectCustom | 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 | 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 | 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 . |
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()
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
}
} -
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) {