# Nakama Friends Sample Project

**URL:** https://heroiclabs.com/docs/sample-projects/unity/nakama-friends/
**Summary:** Friends management sample project for Unity games
**Keywords:** nakama friends, unity sdk, nakama friends controller, list friends async, add friends async, delete friends async, block friends async, friend requests, friend states
**Categories:** unity, sample-projects, nakama-friends

---


## Code overview

#### Main controller (`NakamaFriendsController.cs`)

Handles all core operations including calling the Nakama Friends API.

**Authentication**

```csharp
// Connects to Nakama server and authenticates with device ID
await AuthenticateWithDevice();
```

**Friends operations**

```csharp
// Fetch friends list by state
var result = await Client.ListFriendsAsync(Session, (int)friendState, limit, cursor);

// Add a friend (or accept a friend request)
await Client.AddFriendsAsync(Session, null, friendsToAdd);

// Remove a friend (or decline a friend request)
await Client.DeleteFriendsAsync(Session, null, friendsToDelete);

// Block a user
await Client.BlockFriendsAsync(Session, null, friendsToBlock);
```

**Friend states**

The system supports four different friend states:

```csharp
public enum FriendState
{
    FRIEND = 0,          // Confirmed friends
    INVITE_SENT = 1,     // Outgoing friend requests
    INVITE_RECEIVED = 2, // Incoming friend requests
    BLOCKED = 3          // Blocked users
}
```

**Real-time notifications**

```csharp
private void OnReceivedNotification(IApiNotification notification)
{
    switch (notification.Code)
    {
        case -2: // Incoming friend request received
            // Update UI for received requests
            break;
        case -3: // Outgoing friend request accepted
            // Update UI for new friends
            break;
    }
}
```

{{<note "important" "Web sockets">}}
This sample project uses web sockets in order to handle real-time updates and make changes to the UI, but they are not required to use the Nakama Friends feature.
{{< / note >}}

#### Record view (`FriendsRecordView.cs`)

A class that displays individual friend entries with different actions based on friend state:

```csharp
public void SetFriend(IApiFriend record)
{
    usernameLabel.text = record.User.Username;
    var state = (NakamaFriendsController.FriendState)record.State;

    // Show/hide buttons based on friend state
    switch (state)
    {
        case NakamaFriendsController.FriendState.FRIEND:
            // Show remove and block buttons
            break;
        case NakamaFriendsController.FriendState.INVITE_RECEIVED:
            // Show add, remove, and block buttons
            break;
        // ... other states
    }
}
```
