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, and status 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 overwrite

    user.update(
    name: String?,
    externalId: String?,
    profileUrl: String?,
    email: String?,
    custom: CustomObject?,
    status: String?,
    type: String?,
    ): PNFuture<User>
  • update() and synchronize

    user.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

ParameterTypeRequired in update() (overwrite)Required in update() (synchronize)Required in updateUser()DefaultDescription
idStringNoNoYesn/aUnique user identifier.
nameStringNoNoNon/aDisplay name for the user (must not be empty or consist only of whitespace characters).
externalIdStringNoNoNon/aUser's identifier in an external system. You can use it to match id with a similar identifier from an external database.
profileUrlStringNoNoNon/aURL of the user's profile picture.
emailStringNoNoNon/aUser's email address.
customObjectCustomNoNoNon/aJSON 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.
statusStringNoNoNon/aTag 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.
typeStringNoNoNon/aTag 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.
updateActionLambda functionNoYesNon/aA 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:

  • name
  • externalId
  • profileUrl
  • email
  • custom
  • status
  • type
API limits

To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.

Output

TypeDescription
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

    // 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
    }
    }
    show all 19 lines
  • update() and synchronize

    // 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
    show all 21 lines
  • 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 single User object.
  • streamUpdatesOn() checks updates on a list of User 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

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
usersCollection<User>NoYesn/aA collection of User objects for which you want to get updates.
callback(user: User?) -> UnitYesNon/aFunction that takes a single User object. It defines the custom behavior to be executed when detecting user changes.
callback(users: Collection<User>) -> UnitNoYesn/aFunction that takes a set of User objects. It defines the custom behavior to be executed when detecting user changes.

Output

TypeDescription
AutoCloseableInterface 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()

    class 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.")
    }
    show all 24 lines

Stop listening to updates on support_agent_15 and support-manager.

  • streamUpdatesOn()

    class 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) {
    show all 29 lines
Last updated on