Friends are a great way to build a social community. Users can add other users to their list of friends, see who is online or when they were last online, chat together in real-time, and interact together in gameplay or collaboration.
Each user builds up a list of friends by who they know already from their social networks, friend requests they send, requests they receive, and who the server recommends they should know. This information is stored in a social graph within the system as a powerful way to interact with other users. Much like how Twitter or Facebook work.
Any social community must be maintained carefully to prevent spam or abuse. To help with this problem it’s also possible for a user to block users they no longer want to communicate with and for the server to ban a user via server-side code to completely disable an account.
Nakama is a common Japanese word that directly translates to friend or comrade. Some believe the word means “people who are considered closer than family”, though that is not a part of the official definition. We feel it expresses the kind of social communities we want developers to build into their games and apps!
A user can add one or more friends by that user’s ID or username. The user added will not be marked as a friend in the list until they’ve confirmed the friend request. The user who receives the request can confirm it by adding the user back.
When a friend request is sent or the user is added an in-app notification will be sent. See the in-app notification section for more info.
When both users have added each other as friends, it’s easy to initiate real-time chat in a 1-on-1 channel. See the real-time chat section for more info.
A user who registers or links their account with Facebook, Steam, or another social network can select to have friends from that network imported into their friend list.
Alternatively, you can enable users to perform this import at any time via Nakama’s support for on-demand importing of friends.
You can list all of a user’s friends, blocked users, friend requests received (invited), and invites they’ve sent. These statuses are returned together as part of the friend list which makes it easy to display in a UI.
A single friends listing will return a page of up to 1000 friends. Pass the returned cursor to subsequent list calls to retrieve more friend pages.
varresult=awaitclient.ListFriendsAsync(session);foreach(varfinresult.Friends){System.Console.WriteLine("Friend '{0}' state '{1}'",f.User.Username,f.State);}
Friendsfriends=client.listFriends(session).get();for(Friendfriend:friends.getFriendsList()){System.out.format("Friend %s state %d",friend.getUser().getUsername(),friend.getState());}
Client
1
2
3
4
5
6
7
8
9
varlist:NakamaAPI.ApiFriendList=yield(client.list_friends_async(session),"completed")iflist.is_exception():print("An error occurred: %s"%list)returnforfinlist.friends:varfriend=fasNakamaAPI.ApiFriendprint("User %s, status %s"%[friend.user.id,friend.state])
Client
1
2
3
4
5
6
7
8
9
varlist:NakamaAPI.ApiFriendList=awaitclient.list_friends_async(session)iflist.is_exception():print("An error occurred: %s"%list)returnforfinlist.friends:varfriend=fasNakamaAPI.ApiFriendprint("User %s, status %s"%[friend.user.id,friend.state])
To list friends of friends for this user. A maximum of 100 results per request is allowed. An optional cursor can be supplied, don’t set to start fetching from the beginning.
A user can remove a friend, reject a received invite, cancel a friend request sent, or unblock a user. If a user is unblocked they are removed from the friend list entirely. To re-add them each user must add the other again.
Similar to how Friend Add works we reuse Friend Remove to cancel or undo whatever friend state is current with another user.
You can stop a user from using 1-on-1 chat or other social features with a user if you block them. The user who wants to block should send the message. They can be unblocked later with a Friend Remove message.
A user who has been blocked will not know which users have blocked them. That user can continue to add friends and interact with other users.
This is best used by a moderator system within your player community. You can assign particular users the ability to send an RPC to permanently ban a user.
Banning a user will prevent them from being able to connect to the server and interact at all in the future, but it does not implicitly logout or disconnect their session.
To ensure a banned user cannot reconnect using a still valid auth token, and that any open socket connections are closed, you must ban the user and logout and disconnect their active session(s). See a complete example on banning users.