nakama 1.1.0 copy "nakama: ^1.1.0" to clipboard
nakama: ^1.1.0 copied to clipboard

Nakama is an open-source scalable game server that lets you focus on building games.

Nakama Dart/Flutter Client #

Nakama is an open-source server designed to power modern games and apps. Features include user accounts, chat, social, matchmaker, realtime multiplayer, and much more.

This is a Flutter client for Nakama written in pure Dart and supports cross platform gaming on iOS, Android, Web and more.

GitHub issues GitHub last commit Pub Version


🚀 Getting Started #

You'll need to setup the server and database before you can connect with the client. The simplest way is to use Docker but have a look at the server documentation for other options.

Installing the SDK #

  1. To use an official release, download source from the releases page and import it into your project.

  2. Add flutter_nakama to your pubspec.yaml:

name: your_game
dependencies:
    flutter:
        sdk: flutter
    nakama: ^1.0.0-dev.6
  1. Use the connection credentials to build a client object:
final client = getNakamaClient(
  host: '127.0.0.1',
  ssl: false,
  serverKey: 'defaultkey',
  grpcPort: 7349, // optional
  httpPort: 7350, // optional
);

Usage #

The client object has many methods to execute various features in the server or open realtime socket connections with the server.

Authenticate #

There's a variety of ways to authenticate with the server. Authentication can create a user if they don't already exist with those credentials. It's also easy to authenticate with a social profile from Google Play Games, Facebook, Game Center, etc.

final session = await getNakamaClient().authenticateEmail(
    email: 'foo@bar.de',
    password: 'mySecurePassword!',
);

print('Hey, you are logged in! UserID: ${session.userId}');

Sessions #

When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a Session object.

session.AuthToken // raw JWT token
session.UserId // User ID
session.Username // Username
session.IsExpired // Boolean indicating session status
session.ExpireTime // in seconds.

It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.

final inOneHour = DateTime.now().add(Duration(hours: 1));

// Check whether a session has expired or is close to expiry.
if (session.isExpired || session.hasExpired(inOneHour)) {
    try {
        // Attempt to refresh the existing session.
        session = await client.sessionRefresh(session);
    } catch (e) {
        // Couldn't refresh the session so reauthenticate.
        session = await client.authenticateDevice(deviceId: deviceId);
    }
}

Requests #

The client includes lots of builtin APIs for various features of the game server. These can be accessed with the async methods. It can also call custom logic as RPC functions on the server. These can also be executed with a socket object.

All requests are sent with a session object which authorizes the client.

final account = await client.getAccount(session);
final username = account.user.username;
final avatarUrl = account.user.avatarUrl;
final userId = account.user.id;

Socket #

The client can create one or more sockets with the server. Each socket can have it's own event listeners registered for responses received from the server.

NakamaWebsocketClient.init(
    host: '127.0.0.1',
    ssl: false,
    token: _session.token,
);

Remember to close the connection after disposing of the app widget:

NakamaWebsocketClient.instance.close();

Documentation #

Dart/Flutter SDK Docs: https://heroiclabs.com/docs/nakama/client-libraries/dart/

Nakama Docs: https://heroiclabs.com/docs/nakama/

Special Thanks #

Thanks to Oliver Brunsmann (@obrunsmann) for his excellent contribution and maintenance of this library.