Skip to content

Querying the Social Graph

Prerequisite

Check if User is a Friend

That is how you check if some user is a friend of the current user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
final String anotherUserId = anotherUser.getId();
GetSocial.User.isFriend(anotherUserId, new Callback<Boolean>() {
    @Override
    public void onSuccess(Boolean isFriend) {
        Log.i("GetSocial", "Are " + GetSocial.User.getId() + " and " + anotherUserId + " friends? " + isFriend);
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while checking friendship: " + exception.getMessage());
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
val anotherUserId = anotherUser.id
GetSocial.User.isFriend(anotherUserId, object : Callback<Boolean> {
    override fun onSuccess(isFriend : Boolean) {
        println("Are ${GetSocial.User.getId()} and ${anotherUserId} friends? ${isFriend}")
    }

    override fun onFailure(exception: GetSocialException) {
        println("Exception while checking friendship, error: ${exception.message}")
    }
})
1
2
3
4
5
6
NSString *anotherUserId = anotherUser.userId;
[GetSocialUser isFriend:anotherUserId success:^(BOOL isFriend) {
    NSLog(@"Are %@ and %@ friends? %@", GetSocialUser.userId, anotherUserId, isFriend ? @"Yes" : @"No");
} failure:^(NSError *error) {
    NSLog(@"Exception while checking friendship: %@", error);
}];
1
2
3
4
5
6
let anotherUserId = anotherUser.userId
GetSocialUser.isFriend(anotherUserId, success: { isFriend in
    print("Are \(GetSocialUser.userId) and \(anotherUserId) friends? \(isFriend ? "Yes" : "No")")
}, failure: { error in
    print("Exception while checking friendship: \(error)")
})
1
2
3
4
5
6
7
8
var anotherUserId = user.Id;
GetSocial.User.IsFriend (anotherUserId, 
    isFriend => {
        Debug.Log ("Are " + GetSocial.User.getId() + " and " + anotherUserId + " friends? " + isFriend);
    },
    error => {
        Debug.Log ("Exception while checking friendship: " + error.Message);
    });
1
2
3
4
5
6
7
8
const anotherUserId = user.userId;
GetSocialUser.isFriend(anotherUserId).then(
    (isFriend) => {
        console.log('Are you and ' + anotherUserId + " friends? " + isFriend);
    }, 
    (error) => {
        console.log('Exception while checking friendship: ' + error);
    });

Get Amount of Friends

You can get the number of friends of the current user with the help of the following method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
GetSocial.User.getFriendsCount(new Callback<Integer>() {
    @Override
    public void onSuccess(Integer friendsCount) {
        // Usually developers update some UI badge, which shows the counter
        Log.i("GetSocial", "Amount of friends for current user: " + friendsCount);
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving friends count: " + exception.getMessage());
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
GetSocial.User.getFriendsCount(object : Callback<Int> {
    override fun onSuccess(friendsCount : Int) {
        // Usually developers update some UI badge, which shows the counter
        println("Amount of friends for current user: ${friendsCount}")
    }

    override fun onFailure(exception: GetSocialException) {
        println("Exception while receiving friends count, error: ${exception.message}")
    }
})
1
2
3
4
5
[GetSocialUser friendsCountWithSuccess:^(int friendsCount) {
    NSLog(@"Amount of friends for current user: %d", friendsCount);
} failure:^(NSError *error) {
    NSLog(@"Exception while receiving friends count: %@", error);
}];
1
2
3
4
5
GetSocialUser.friendsCount(withSuccess: { friendsCount in
    print("Amount of friends for current user: \(friendsCount)")
}, failure: { error in
    print("Exception while receiving friends count: \(error)")
})
1
2
3
4
5
6
7
GetSocial.User.GetFriendsCount ( 
    friendsCount => {
        Debug.Log ("Amount of friends for current user: " + friendsCount);
    },
    error => {
        Debug.Log ("Exception while receiving friends count: " + error.Message);
    });
1
2
3
4
5
6
7
GetSocialUser.getFriendsCount().then(
    (friendsCount) => {
        console.log('Amount of friends for current user: ' + friendsCount);
    }, 
    (error) => {
        console.log('Exception while receiving friends count: ' + error);
    });

Get List of Friends

You can get the list of all friends of the current user page by page.
Use limit and offset to traverse multiple pages of friends.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int limit = 100;
int offset = 0;
GetSocial.User.getFriends(offset, limit, new Callback<List<PublicUser>>() {
    @Override
    public void onSuccess(List<PublicUser> friends) {
        for (PublicUser friend : friends) {
            Log.i("GetSocial", "Received a friend: " + friend.getId() + " " + friend.getDisplayName());
        }
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving friends: " + exception.getMessage());
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
val limit = 100
val offset = 0
GetSocial.User.getFriends(offset, limit, object : Callback<List<PublicUser>> {
    override fun onSuccess(friends: List<PublicUser>) {
        friends.forEach {friend ->
            println("Received a friend: ${friend.id} ${friend.displayName}")
        }
    }

    override fun onFailure(exception: GetSocialException) {
        println("Exception while receiving friends, error: ${exception.message}")
    }
})
1
2
3
4
5
6
7
8
9
int offset = 0;
int limit = 100;
[GetSocialUser friendsWithOffset:offset limit:limit success:^(NSArray<GetSocialPublicUser *> *friends) {
    for (GetSocialPublicUser *friend in friends) {
        NSLog(@"Received a friend: %@ %@", friend.userId, friend.displayName);
    }
} failure:^(NSError *error) {
    NSLog(@"Exception while receiving friends: %@", error);
}];
1
2
3
4
5
6
7
8
9
let offset: Int32 = 0
let limit: Int32 = 100
GetSocialUser.friends(withOffset: offset, limit: limit, success: { friends in
    for friend in friends {
        print("Received a friend: \(friend.userId) \(friend.displayName)")
    }
}, failure: { error in
    print("Exception while receiving friends: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var offset = 0;
var limit = 100;
GetSocial.User.GetFriends(offset, limit,
    friends => {
        foreach (var friend in friends) {
            Debug.Log ("Received a friend: " + friend.Id + " " + friend.DisplayName);
        }
    },
    error => {
        Debug.Log ("Exception while receiving friends: " + error.Message);
    });
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const offset = 0;
const limit = 100;
GetSocialUser.getFriends(offset, limit).then(
    (friends) => {
        friends.forEach(friend => {
            console.log('Received a friend: ' + friend.userId + ' ' + friend.displayName);
        });
    }, 
    (error) => {
        console.log('Exception while receiving friends: ' + error);
    });

Suggested Friends

When the user is building his social graph and creating connections with other users, GetSocial backend is finding the best possible options to make new friendship for your user. As a result, we make a list of users, which are not directly connected to the user but have mutual friends with him. That means that these users could be friends with your user in real life, but they have not met in your game yet. With Suggested Friends functionality you can make building user’s social graph process much faster. Use it as described below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int limit = 10;
int offset = 0;
GetSocial.User.getSuggestedFriends(offset, limit, new Callback<List<SuggestedFriend>>() {
    @Override
    public void onSuccess(List<SuggestedFriend> friends) {
        for (SuggestedFriend friend : friends) {
            Log.i("GetSocial", "Possible friend: " + friend.getDisplayName() + " (" + friend.getMutualFriendsCount() + " mutual friends)");
        }
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving suggested friends: " + exception.getMessage());
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
val limit = 10
val offset = 0
GetSocial.User.getSuggestedFriends(offset, limit, object : Callback<List<SuggestedFriend>> {
    override fun onSuccess(friends: List<SuggestedFriend>) {
        friends.forEach {friend ->
            println("Possible friend: ${friend.displayName} (${friend.mutualFriendsCount} mutual friends)")
        }
    }

    override fun onFailure(exception: GetSocialException) {
        println("Exception while receiving suggested friends, error: ${exception.message}")
    }
})
1
2
3
4
5
6
7
8
9
int offset = 0;
int limit = 10;
[GetSocialUser suggestedFriendsWithOffset:offset limit:limit success:^(NSArray<GetSocialSuggestedFriend *> *friends) {
    for (GetSocialSuggestedFriend *friend in friends) {
        NSLog(@"Possible friend: %@ (%d mutual friends)", friend.displayName, friend.mutualFriendsCount);
    }
} failure:^(NSError *error) {
    NSLog(@"Exception while receiving suggested friends: %@", error);
}];
1
2
3
4
5
6
7
8
9
let offset: Int32 = 0
let limit: Int32 = 10
GetSocialUser.suggestedFriends(withOffset: offset, limit: limit, success: { friends in
    for friend in friends {
        print("Possible friend: \(friend.displayName) (\(friend.mutualFriendsCount) mutual friends)")
    }
}, failure: { error in
        print("Exception while receiving suggested friends: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var offset = 0;
var limit = 10;
GetSocial.User.GetSuggestedFriends(offset, limit,
    friends => {
        foreach (var friend in friends) {
            Debug.Log ("Possible friend: " + friend.DisplayName + "(" + friend.MutualFriendsCount + " mutual friends)");
        }
    },
    error => {
        Debug.Log ("Exception while receiving suggested friends: " + error.Message);
    });
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const offset = 0;
const limit = 0;
GetSocialUser.getSuggestedFriends(offset, limit).then(
    (suggestedFriends) => {
        suggestedFriends.forEach(suggestedFriend => {
            console.log('Possible friend: ' + suggestedFriend.userId + ' ' + suggestedFriend.displayName);
        });
    }, 
    (error) => {
        console.log('Exception while receiving suggested friends: ' + error);
    });

Next steps

  • Learn how to create a chat with your friends here.
  • Learn how to send notifications to friends here.

Give us your feedback! Was this article helpful?

😀 🙁