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

ParameterTypeRequired in update()Required in updateUser()DefaultDescription
idStringNoYesn/aUnique user identifier.
nameStringNoNon/aDisplay name for the user (must not be empty or consist only of whitespace characters).
externalIdStringNoNon/aUser's identifier in an external system. You can use it to match id with a similar identifier from an external database.
profileUrlStringNoNon/aURL of the user's profile picture.
emailStringNoNon/aUser's email address.
customObjectCustomNoNon/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.
statusStringNoNon/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.
typeStringNoNon/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.
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()

    // 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
  • 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