Browse SDKs · React Native
SDKsReact Native

Get the friend list

Get the friend list with the OpenIM React Native SDK.

Copy

Call getFriendListPage() to retrieve the current user's friends page by page.

const friends = await OpenIMSDK.getFriendListPage({
  offset: 0,
  count: 50,
  filterBlack: true,
});

Parameter description

ParameterTypeRequiredDescription
offsetnumberYesPagination offset. Use 0 for the first page.
countnumberYesNumber of friends to request.
filterBlackbooleanNoWhether to filter out blocked users.

The method returns the current page as FriendUserItem[]. Increase offset by the number of returned items to load the next page. Query the list again after friends are added or deleted.

Friend fields

FriendUserItem contains the following fields:

FieldTypeDescription
userIDstringFriend user ID.
nicknamestringFriend nickname.
faceURLstringFriend avatar URL.
remarkstringRemark set for the friend by the current user.
isPinnedbooleanWhether the friend is pinned.
ownerUserIDstringUser ID that owns the friendship.
operatorUserIDstringUser ID that most recently changed the relationship.
addSourcenumberSource from which the friendship was added.
createTimenumberTime when the friendship was created.
exstringFriendship extension field.
attachedInfostringInformation attached by the SDK.

remark, isPinned, and ex belong to the friendship and are not account-level user information.

Listen for friend changes

When a friendship changes, OnFriendAdded, OnFriendInfoChanged, or OnFriendDeleted is emitted with the corresponding friend information:

const handleFriendAdd = (friend: FriendUserItem) => {
  // Add the friend
};
const handleFriendChanged = (friend: FriendUserItem) => {
  // Update the friend by friend.userID
};
const handleFriendDeleted = (friend: FriendUserItem) => {
  // Remove the friend by friend.userID
};

// Add listeners
OpenIMSDK.on(OpenIMEvent.OnFriendAdded, handleFriendAdd);
OpenIMSDK.on(OpenIMEvent.OnFriendInfoChanged, handleFriendChanged);
OpenIMSDK.on(OpenIMEvent.OnFriendDeleted, handleFriendDeleted);

// Remove listeners
OpenIMSDK.off(OpenIMEvent.OnFriendAdded, handleFriendAdd);
OpenIMSDK.off(OpenIMEvent.OnFriendInfoChanged, handleFriendChanged);
OpenIMSDK.off(OpenIMEvent.OnFriendDeleted, handleFriendDeleted);

Remove these listeners when logging out, switching accounts, or tearing down the friend-list state.