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

icon

Under the hood


These methods take the following parameters:

  • update()

    user.update({
    name?: string,
    externalId?: string,
    profileUrl?: string,
    email?: string,
    custom?: ObjectCustom,
    status?: string,
    type?: string,
    updated?: string
    }): Promise<User>
  • updateUser()

    chat.updateUser(
    id: string,
    {
    name?: string,
    externalId?: string,
    profileUrl?: string,
    email?: string,
    custom?: ObjectCustom,
    status?: string,
    type?: string,
    updated?: string
    }): Promise<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. App Context filtering language doesn’t support filtering by custom properties.
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.
updatedstringNoNon/aLast time the User object was changed.
API limits

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

Output

TypeDescription
Promise<User>Returned object containing the updated user metadata.

Errors

Whenever the user ID is required, and you try to update the user without providing their ID, you will receive the ID is required error. If you try to edit a user that doesn't exist, you will receive the User with this ID does not exist error.

Basic usage

Change the link to the user's support_agent_15 LinkedIn profile to https://www.linkedin.com/mkelly_vp2.

  • update()

    // reference the "user" object
    const user = await chat.getUser("support_agent_15")
    // invoke the "update()" method on the "user" object
    await user.update(
    {
    custom: {
    linkedInUrl: "https://www.linkedin.com/mkelly_vp2"
    }
    }
    )
  • updateUser()

    // reference the "chat" object and invoke the "updateUser()" method
    const user = await chat.updateUser(
    "support_agent_15",
    {
    custom: {
    linkedInUrl: "https://www.linkedin.com/mkelly_vp2"
    }
    }
    )

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

icon

Under the hood


These methods take the following parameters:

  • streamUpdates()

    user.streamUpdates(
    callback: (user: User) => unknown
    ): () => void
  • streamUpdatesOn()

    static User.streamUpdatesOn(
    users: User[],
    callback: (users: User[]) => unknown
    ): () => void

Input

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
usersUser[]NoYesn/aArray of User objects for which you want to get updates.
callbackn/aYesYesn/aCallback function passed as a parameter to both methods. It defines the custom behavior to be executed when detecting user metadata changes.
 → userUserYesNon/aReturned User object with the updated data.
 → usersUser[]NoYesn/aReturned array of User objects with the updated data.

Output

TypeDescription
() => voidFunction you can call to disconnect (unsubscribe) from the channel and stop receiving objects events.

Errors

Whenever a list of User objects is required as a parameter, and you try to get updates on users without specifying their list, you will receive the Cannot stream user updates on an empty list error.

Basic usage

Get updates on support_agent_15.

  • streamUpdates()

    const user = await chat.getUser("support_agent_15")
    user.streamUpdates((user) => {
    console.log("Updated user: ", user)
    })

Get updates on support_agent_15 and support-manager.

  • streamUpdatesOn()

    const agentUser = await chat.getUser("support_agent_15")
    const managerUser = await chat.getUser("support-manager")
    User.streamUpdatesOn([agentUser, incidentChannel], (users) => {
    console.log("Updated users: ", users)
    })

Other examples

Stop listening to updates on support_agent_15.

  • streamUpdates()

    const user = await chat.getUser("support_agent_15")
    const stopUpdates = user.streamUpdates(/* handle update callback */)
    // after some time...
    stopUpdates()

Stop listening to updates on support_agent_15 and support-manager.

  • streamUpdatesOn()

    const agentUser = await chat.getUser("support_agent_15")
    const managerUser = await chat.getUser("support-manager")
    const stopUpdates = User.streamUpdatesOn([agentUser, incidentChannel], /* handle update callback */)
    // after some time...
    stopUpdates()
Last updated on