logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Friend management


Overview

ZIM SDK supports friend management, allowing users to directly add and delete friends, view their friends list, send friend requests to other users, accept or reject friend requests, view friend request lists, check their friend relationship with other users, and query or modify friend information.

Each user can have a maximum of 3000 friends.

Basic functions

By calling the on API, you can register callbacks to listen for friend-related events, including: friendListChanged, friendApplicationListChanged, friendApplicationUpdated and friendInfoUpdated.

Untitled
// Friend list changed callback
zim.on('friendListChanged', (zim, data) => {
    // todo
});

// Friend info updated callback
zim.on('friendInfoUpdated', (zim, data) => {
    // todo
});

// Friend application list changed callback
zim.on('friendApplicationListChanged', (zim, data) => {
    // todo
});

// Friend application updated callback
zim.on('friendApplicationUpdated', (zim, data) => {
    // todo
});
1
Copied!

Add friends directly

After logging in to the ZIM SDK, users can use the addFriend API to directly add other users as friends without requiring their consent. This allows users to set friend remarks and attributes for the added user.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of adding a friend is returned through the ZIMFriendAddedResult .

Untitled
// Add friends directly
zim.addFriend('userID', { wording: 'Hello!', friendAlias: 'Mark', friendAttributes: { k0: 'SZ' } }).then(res => {
    const friendInfo = res.friendInfo;
});
1
Copied!

After successfully adding a friend, the related users will receive the callback friendListChanged to notify them that the user has become a friend.

Batch deleting friends

After logging in to the ZIM SDK, users can use the deleteFriends API to batch delete up to 20 friends in a one-way or two-way deletion manner.

In this example, one-way deletion and two-way deletion are explained.

  • One-way deletion: If user A deletes user B in a one-way manner, user B is no longer a friend of user A, but not vice versa.
  • Two-way deletion: If user A deletes user B in a two-way manner, they are no longer friends with each other.

The result of deleting friends is returned through ZIMFriendsDeletedResult.

Untitled
// Batch delete friends
// type 0: bilateral deletion
// type 1: unilateral deletion
zim.deleteFriends(['userID1', 'userID2'], { type: 0 }).then(res => {
    const errorUserList = res.errorUserList;
})
1
Copied!

After successfully deleting a friend, depending on the deletion type, the related users will receive the callback friendListChanged to notify them that the user is no longer a friend.

Send friend requests

After logging in to the ZIM SDK, users can use the sendFriendApplication API to send friend requests to other users and set friend remarks and attributes.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of sending a friend request is returned through ZIMFriendApplicationSentResult.

Untitled
// Receive friend application list callback
zim.on('friendApplicationListChanged', (zim, data) => {
    // action is 0: new
    const { action, applicationList } = data;
});
1
Copied!

The target user will receive the friendApplicationListChanged callback, indicating that a user has requested to become friends. The user can choose to accept or reject the request within 7 days.

Note

If you need to adjust the friend application validity period, please contact the ZEGOCLOUD technical support team.

Accept friend requests

After logging in to ZIM SDK, users can call the acceptFriendApplication API, passing the ID of the user who initiated the request, to accept a friend request.

The result of accepting a friend request is returned through ZIMFriendApplicationAcceptedResult.

Untitled
// Accept friend application
zim.acceptFriendApplication('userID', { friendAlias: 'Mark', friendAttributes: { k0: 'SZ' } }).then(res => {
    const friendInfo = res.friendInfo;
})
1
Copied!

Both the requesting user and the requested user will not only receive the friendApplicationUpdated to know that the other user has become their friend.

Reject friend requests

After logging in to ZIM SDK, users can reject friend requests by using the rejectFriendApplication API.

The result of rejecting a friend request is returned through ZIMFriendApplicationRejectedResult.

Untitled
// Reject friend application
zim.rejectFriendApplication('userID', {} }).then(res => {
    const userInfo = res.userInfo;
})
1
Copied!

Both the requesting user and the requested user will receive the friendApplicationUpdated callback to know that the application has been rejected.

Query friend list

After logging in to the ZIM SDK, users can use the queryFriendList API to retrieve the paginated complete friends list.

The query results are returned through ZIMFriendListQueriedResult, and the returned friends list is sorted in descending order based on the time of friend relationship creation.

Untitled
// Query friend information list
zim.queryFriendList({ count: 100, nextFlag: 0 }).then(res => {
    const { friendList, nextFlag } = res;
});
1
Copied!

Query friend request list

After logging in to the ZIM SDK, users can use the queryFriendApplicationList API to retrieve the friend request list and understand the status of each request. The friend request list includes both the requests initiated by the user to other users and the requests initiated by other users to the user. The query results are returned through ZIMFriendApplicationListQueriedResult.

Untitled
// Query friend application information list
zim.queryFriendApplicationList({ count: 100, nextFlag: 0 }).then(res => {
    const { applicationList, nextFlag } = res;
});
1
Copied!

More functions

Update friend alias

After logging in to the ZIM SDK, users can update the alias they have for a friend using the updateFriendAlias API.

The result of the update is returned through ZIMFriendAliasUpdatedResult.

Untitled
// Update friend remark
zim.updateFriendAlias('New remark', 'userID' }).then(res => {
    const friendInfo = res.friendInfo;
});
1
Copied!

After a successful update, the user can receive the friendInfoUpdated callback to know that the friend's information has been updated.

Update friend attributes

After logging in to the ZIM SDK, users can update friend attributes using the updateFriendAttributes API.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of updating friend attributes is returned through ZIMFriendAttributesUpdatedResult.

Untitled
// Update friend attributes
zim.updateFriendAttributes({ k1: 'v1', k2: 'v2' } , 'userID' }).then(res => {
    const friendInfo = res.friendInfo;
});
1
Copied!

After logging in to the ZIM SDK, users can update friend attributes using the friendInfoUpdated API.

Check friend relationships

After logging in to the ZIM SDK, users can use the checkFriendsRelation API to batch check their friend relationships with up to 20 other users.

ZEGOCLOUD allows one-way or two-way check of friendships. In this example, the friendship between users A and B is checked.

  • One-way check: Only checks whether user B is in the friend list of user A.
  • Two-way check: Checks whether users A and B are in the friend list of the other.

The result of checking friend relationships is returned through ZIMFriendsRelationCheckedResult.

Untitled
// Check friend relationship with other users
// type is 0: bidirectional
// type is 1: unidirectional
zim.checkFriendsRelation(['userID1', 'userID2'], { type: 0 } }).then(res => {
    const { relationInfos, errorUserList } = res;
})
1
Copied!

Batch query friend information

After logging in to the ZIM SDK, users can use the queryFriendsInfo API to batch query information for up to 20 friends.

The result of batch querying friend information is returned through ZIMFriendsInfoQueriedResult.

Untitled
// Batch query friend information
zim.queryFriendsInfo(['userID1', 'userID2']).then(res => {
    const { friendInfos, errorUserList } = res;
});
1
Copied!

Search for friends

After logging in to the ZIM SDK, users can use the searchLocalFriends API to search for friends based on their usernames and aliases. Users can provide up to 5 keywords to search for friends who match all the provided keywords.

The search results are returned through ZIMFriendsSearchedResult.

Untitled
zim.searchLocalFriends({ keywords: ['a', 'b'], isAlsoMatchFriendAlias: true, count: 100, nextFlag: 0 }).then(res => {
    // todo
});
1
Copied!

Previous

Blacklist management

Next

Online Status Subscription