Browse SDKs · WASM
SDKsWASM

Get the friend list

Page through the current user’s friend list.

Copy

Parameters

ParameterTypeRequiredDescription
offsetnumberYesPagination offset. Pass 0 for the first page.
countnumberYesNumber of friends to request.
filterBlackbooleanNoWhether to exclude blacklisted users from the results.
const { data: friends } = await openimsdk.getFriendListPage({
  offset: 0,
  count: 50,
  filterBlack: true,
});

After the Promise succeeds, data contains the current page of FriendUserItem[]. Increase offset by the requested item count to load the next page. Reset pagination after a friend is added or removed.

Friend profile fields

FriendUserItem describes the current account's relationship with one friend:

FieldTypeDescription
userIDstringThe friend's user ID and the stable identifier in the friend list.
nicknamestringThe friend's account-level nickname.
faceURLstringThe friend's account-level avatar URL.
remarkstringA remark the current account assigned to this friend.
isPinnedbooleanWhether the friend is pinned in the contacts list.
ownerUserIDstringThe user ID that owns this friendship, normally the current account.
operatorUserIDstringThe user ID that created or updated the relationship.
addSourcenumberValue describing the source through which the friendship was added.
createTimenumberTime when the friendship was created.
exstringFriendship extension string.
attachedInfostringSDK attachment data. Parse it only according to a confirmed application contract.

nickname and faceURL are snapshots of the account profile. remark, isPinned, ex, and attachedInfo belong to the friendship. Do not overwrite a non-friend's PublicUserItem with a FriendUserItem, and do not write a friend remark back to the account nickname.

Synchronize friend changes

This page owns the complete listeners for OnFriendAdded, OnFriendInfoChanged, and OnFriendDeleted. The query establishes a snapshot; events merge incremental changes by userID.

import { CbEvents } from '@openim/wasm-client-sdk';

const handleFriendChanged = ({ data }) => mergeFriend(data);
const handleFriendDeleted = ({ data }) => removeFriend(data.userID);

openimsdk.on(CbEvents.OnFriendAdded, handleFriendChanged);
openimsdk.on(CbEvents.OnFriendInfoChanged, handleFriendChanged);
openimsdk.on(CbEvents.OnFriendDeleted, handleFriendDeleted);

function removeFriendListeners() {
  openimsdk.off(CbEvents.OnFriendAdded, handleFriendChanged);
  openimsdk.off(CbEvents.OnFriendInfoChanged, handleFriendChanged);
  openimsdk.off(CbEvents.OnFriendDeleted, handleFriendDeleted);
}

Call removeFriendListeners() when signing out, switching accounts, or destroying the contacts state layer.