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? = nil,
    externalId: String? = nil,
    profileUrl: String? = nil,
    email: String? = nil,
    custom: [String: JSONCodableScalar]? = nil,
    status: String? = nil,
    type: String? = nil,
    completion: ((Swift.Result<UserImpl, Error>) -> Void)? = nil
    )
  • updateUser()

    chat.updateUser(
    id: String,
    name: String? = nil,
    externalId: String? = nil,
    profileUrl: String? = nil,
    email: String? = nil,
    custom: [String: JSONCodableScalar]? = nil,
    status: String? = nil,
    type: String? = nil,
    completion: ((Swift.Result<UserImpl, Error>) -> Void)? = nil
    )

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.
custom[String: JSONCondableScalar]NoNon/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
((Swift.Result<UserImpl, Error>) -> Void)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()

    // Create PubNub configuration
    let pubNubConfiguration = PubNubConfiguration(
    publishKey: "your-publish-key",
    subscribeKey: "your-subscribe-key",
    userId: "your-user-id"
    // Add other required parameters
    )

    // Create Chat configuration
    let chatConfiguration = ChatConfiguration(
    // Fill in the necessary parameters for ChatConfiguration
    )

    // Create ChatImpl instance
    let chat = ChatImpl(
    show all 56 lines
  • updateUser()

    // Reference the "chat" object and invoke the "updateUser()" method
    chat.updateUser(
    id: userId,
    profileUrl: "https://www.linkedin.com/mkelly_vp2"
    ) { result in
    switch result {
    case let .success(updatedUser):
    debugPrint("User profile updated on Chat object: \(updatedUser)")
    case let .failure(error):
    debugPrint("Failed to update user profile on Chat object: \(error)")
    }
    }

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: @escaping ((UserImpl?) -> Void)) -> AutoCloseable
  • streamUpdatesOn() (static)

    user.streamUpdatesOn(users: [UserImpl], callback: @escaping (([UserImpl]) -> Void)) -> AutoCloseable

Input

ParameterTypeRequired in streamUpdates()Required in streamUpdatesOn()DefaultDescription
users[UserImpl]NoYesn/aA collection of UserImpl objects for which you want to get updates.
callback(user: UserImpl?) -> UnitYesNon/aFunction that takes a single UserImpl object. It defines the custom behavior to be executed when detecting user changes.
callback(users: [UserImpl]) -> UnitNoYesn/aFunction that takes an array of UserImpl 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()

    // Create PubNub configuration
    let pubNubConfiguration = PubNubConfiguration(
    publishKey: "your-publish-key",
    subscribeKey: "your-subscribe-key",
    userId: "your-user-id"
    )

    // Create Chat configuration
    let chatConfiguration = ChatConfiguration(
    // Fill in the necessary parameters for ChatConfiguration
    )

    // Create ChatImpl instance
    let chat = ChatImpl(
    chatConfiguration: chatConfiguration,
    show all 63 lines

Get updates on support_agent_15 and support-manager.

  • streamUpdatesOn()

    // Assuming you have a "chat" instance available and a "UserImpl" representation of "user1", "user1" and "support_agent_15"
    let user1 = UserImpl(chat: chat, id: "user1")
    let user2 = UserImpl(chat: chat, id: "user2")
    let support_agent_15 = UserImpl(chat: chat, id: "support_agent_15")

    // Subscribing to updates for multiple users
    let autoCloseableUsersUpdates = UserImpl.streamUpdatesOn(users: [user1, user2, support_agent_15]) { updatedUsers in
    debugPrint("Users updated: \(updatedUsers.map { $0.id })")
    }

    // Stop listening to updates
    autoCloseableUsersUpdates.close()
Last updated on