Building Social Graph Relationships

Prerequisite

Overview

GetSocial SDK exposes all the methods needed to import existing connections between users or gradually add/remove connections in the graph. All methods can operate on GetSocial user IDs or user IDs from external identities like Facebook, or your identity provider.

Importing Friends

If your app already has a concept of friends or any kind of the relationship between users you can import this data into GetSocial Social Graph to leverage friend suggestions or advanced segmentation in Smart Audiences.

Important

If you import friends with setFriends(...) methods, it will overwrite existing connections. You have to ensure you call it only once. Use addFriends(...) to add new user connections.

1
2
3
4
5
6
7
val users = UserIdList.create("user1", "user2")

Communities.setFriends(users, { friendsCount: Int ->
    Log.d("Communities", "Updated friends, new friends count: $friendsCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to set friends: $error")
})
1
2
3
4
5
6
let userIds = UserIdList.create(["user1", "user2"])
Communities.setFriends(userIds, success: { friendsNumber in
    print("Friends successfully replaced.")
}, failure: { error in
    print("Error while replacing friends: \(error)")
})
1
2
3
4
5
6
7
8
var userIds = UserIdList.Create("user1", "user2");
Communities.SetFriends(userIds,
    (friendsNumber) => {
        Debug.Log("Friends successfully replaced");
    },
    (error) => {
        Debug.Log("Failed to replace friends, error: " + error);
    });
1
2
3
4
var userIds = UserIdList.create(['user1', 'user2']);
Communities.setFriends(userIds)
    .then((friendsNumber) => print('Friends successfully replaced'))
    .catchError((error) => print('Failed to replace friends, error: $error'));
1
2
3
4
5
6
7
const userIds = UserIdList.create(['user1', 'user2']);
Communities.setFriends(userIds)
    .then((friendsNumber) => {
        console.log('Friends successfully replaced');
    }, (error) => {
        console.log('Failed to replace friends, error: ' + error.message);
    });

Adding a Friend

You can make any user become a friend of the current user. If the operation succeeds, both of them will be friends. If you are trying to add a user, that is your friend already, the operation will succeed, nothing will be changed, and you will just receive the current friends count.

1
2
3
4
5
6
7
val users = UserIdList.create("user1", "user2")

Communities.addFriends(users, { friendsCount: Int ->
    Log.d("Communities", "Added friends, new friends count: $friendsCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to add friends: $error")
})
1
2
3
4
5
6
let userIds = UserIdList.create(["user1", "user2"])
Communities.addFriends(userIds, success: { friendsCount in
    print("Successfully added friends. Total friend count: \(friendsCount)")
}, failure: { error in
    print("Error while adding friends: \(error)")
})
1
2
3
4
5
6
7
8
var userIds = UserIdList.Create("user1", "user2");
Communities.AddFriends(userIds,
    (friendsNumber) => {
        Debug.Log("Friends successfully added, total number of friends: " + friendsNumber);
    },
    (error) => {
        Debug.Log("Failed to add friends, error: " + error);
    });
1
2
3
4
var userIds = UserIdList.create(['user1', 'user2']);
Communities.addFriends(userIds)
    .then((friendsNumber) => print('Friends successfully added, total number of friends: $friendsNumber'))
    .catchError((error) => print('Failed to add friends, error: $error'));
1
2
3
4
5
6
7
const userIds = UserIdList.create(['user1', 'user2']);
Communities.addFriends(userIds)
    .then((friendsNumber) => {
        console.log('Friends successfully added, total number of friends: ' + friendsNumber);
    }, (error) => {
        console.log('Failed to add friends, error: ' + error.message);
    });

Removing a Friend

You can remove any user from the current user’s friend list by user’s unique ID. If you are trying to remove a user, who’s not on the friend list, the operation will succeed, nothing will be changed, and you will just receive the current friends count.

1
2
3
4
5
6
7
val users = UserIdList.create("user1", "user2")

Communities.removeFriends(users, { friendsCount: Int ->
    Log.d("Communities", "Removed friends, new friends count: $friendsCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to remove friends: $error")
})
1
2
3
4
5
6
let userIds = UserIdList.create(["user1", "user2"])
Communities.removeFriends(userIds, success: { friendsCount in
    print("Successfully removed friends. Total friends count: \(friendsCount)")
}, failure: { error in
    print("Error while removing friends: \(error)")
})
1
2
3
4
5
6
7
8
var userIds = UserIdList.Create("user1", "user2");
Communities.RemoveFriends(userIds,
    (friendsNumber) => {
        Debug.Log("Friends successfully removed, total number of friends: " + friendsNumber);
    },
    (error) => {
        Debug.Log("Failed to remove friends, error: " + error);
    });
1
2
3
4
var userIds = UserIdList.create(['user1', 'user2']);
Communities.removeFriends(userIds)
    .then((friendsNumber) => print('Friends successfully removed, total number of friends: $friendsNumber'))
    .catchError((error) => print('Failed to remove friends, error: $error'));
1
2
3
4
5
6
7
const userIds = UserIdList.create(['user1', 'user2']);
Communities.removeFriends(userIds)
    .then((friendsNumber) => {
        console.log('Friends successfully removed, total number of friends: ' + friendsNumber);
    }, (error) => {
        console.log('Failed to remove friends, error: ' + error.message);
    });

Next steps

  • Learn how to query social graph here

Give us your feedback! Was this article helpful?

😀 🙁