Guide Multiplayer Nakama Tutorial

Tutorial: Making a Multiplayer Game with Nakama and Unity: Part 2/3

Authentication

In the previous post, we focused on setting Nakama and all its components up. Here we will cover one of the crucial elements of working with multiplayer – authentication, which will allow us to send messages and receive them from the server.

Session Authentication

Almost every method provided by the Nakama plugin for Unity used to communicate with our server requires the user to start and maintain their session. Before we can start a session, we need to create Nakama. Client object which contains a list of useful methods to send data to our server.

With our Client object ready, we can authenticate our device Nakama supports multiple ways of authentication:

  • by using a unique ID, assigned to every device,
  • by using email and password,
  • doing it with your Facebook, Google, Game Center and/or Steam accounts,
  • custom authentication system.

Because of the structure of our Jolly Rogers demo game, the best suit was to use device ID and Facebook authentication methods.

As you can see, AuthenticateDeviceIdAsync method shown above is an asynchronous task – instead of events and callbacks, Nakama relies on Tasks. Whenever we await an asynchronous function with to resolve within an async method, Unity will create a new thread and wait until the blocking task returns. This way the main thread is not suspended, which allows us to play the game (send and receive input) while our message is being sent to the server, processed and returned.

Communicating via the internet is not always granted, that’s why calling AuthenticateDeviceAsync method might throw an exception. To determine what happened we check the error status code and message – in some cases, the exception wasn’t thrown by poor network signal, but rather by the server which received an invalid data. Here we can check if the received error is HttpStatusCode.NotFound, in which case the server told us that the user tried to authenticate an account that doesn’t exist yet – we need to create it first.

In both cases, we called Client.AuthenticateDeviceAsync – they differ only in the last, third parameter, which, when set to true, tells the server, that if no account is bound to this device’s ID, create a new account and bind sender’s ID to it. We could let the server create an account during the first call, however, this way we can implement custom logic for when the user logs in for the first time (e.g. show avatar selection panel).

Rather than authenticating every time a user opens our game, it is recommended to authenticate on the first program run and after the session expiration. We can save our session auth code to PlayerPrefs and restore it whenever the user logs in.

The default session duration is set to 60 seconds – this might suit applications, in which we value security. However, the game industry doesn’t always require that much security, that’s why some studios extend their session durations to several weeks, months or even years. We can extend it by using a YAML config file, which should be included in your bound volumes folder, specified in the chapter Quickstart.

Facebook Authentication

Nakama plugin for Unity doesn’t come with Facebook SDK, or any other external API. That’s why we have to download the Facebook plugin for Unity ourselves. Before we can authenticate by using a Facebook account, we need to walk the user through the Facebook connection process

Because Facebook SDK is an external plugin, we have to handle multiple cases where the authentication process might fail, e.g. when the user cancels the Facebook login, connection to Facebook failed or a given Facebook token is already assigned to another account.

The yellow line shows the code provided by Nakama plugin, other parts are used to determine what happened in case of an exception.

Summary

Authenticated users can communicate with the server for as long as their session doesn’t expire. For easy access, we can store the authentication token in the PlayerPrefs and restore it upon logging in. The next post will cover some of the features Nakama has to offer for our server, how to implement them, as well as some caveats regarding these features and how to overcome them.