You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Nakama](https://github.com/heroiclabs/nakama) is an open-source scalable game server. This is a Flutter client for Nakama written in pure dart and supports cross platform gaming on iOS, Android, Web and more.
3
+
[Nakama](https://github.com/heroiclabs/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](https://heroiclabs.com).
You need a Nakama instance for developing. I suggest setting up Nakama with **Docker Compose**. You find a tutorial here: [Install Nakama with Docker Compose](https://heroiclabs.com/docs/install-docker-quickstart/) and here [Installation & Setup](installation-and-setup.md).
14
+
You'll need to setup the server and database before you can connect with the client. The simplest way is to use [Docker](https://heroiclabs.com/docs/nakama/getting-started/install/docker/) but have a look at the [server documentation](https://heroiclabs.com/docs/nakama/getting-started/install/) for other options.
15
+
16
+
### Installing the SDK
17
+
18
+
1. To use an official release, download source from the [releases page](https://github.com/heroiclabs/nakama-dart/releases) and import it into your project.
19
+
20
+
2. Add flutter\_nakama to your `pubspec.yaml`:
14
21
15
-
### 2. Add flutter\_nakama pre-release to pubspec.yaml.
16
22
```yaml
17
23
name: your_game
18
24
dependencies:
@@ -21,7 +27,8 @@ dependencies:
21
27
nakama: ^1.0.0-dev.6
22
28
```
23
29
24
-
### 3. Create nakama base client
30
+
3. Use the connection credentials to build a client object:
31
+
25
32
```dart
26
33
final client = getNakamaClient(
27
34
host: '127.0.0.1',
@@ -31,8 +38,15 @@ final client = getNakamaClient(
31
38
httpPort: 7350, // optional
32
39
);
33
40
```
34
-
### 4. Use the SDK
35
-
For example start with logging into an user account:
41
+
42
+
## Usage
43
+
44
+
The client object has many methods to execute various features in the server or open realtime socket connections with the server.
45
+
46
+
### Authenticate
47
+
48
+
There's a variety of ways to [authenticate](https://heroiclabs.com/docs/authentication) 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.
49
+
36
50
```dart
37
51
final session = await getNakamaClient().authenticateEmail(
When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `Session` object.
62
+
63
+
```dart
64
+
session.AuthToken // raw JWT token
65
+
session.UserId // User ID
66
+
session.Username // Username
67
+
session.IsExpired // Boolean indicating session status
68
+
session.ExpireTime // in seconds.
69
+
```
70
+
71
+
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.
72
+
73
+
```dart
74
+
final inOneHour = DateTime.now().add(Duration(hours: 1));
75
+
76
+
// Check whether a session has expired or is close to expiry.
77
+
if (session.isExpired || session.hasExpired(inOneHour)) {
78
+
try {
79
+
// Attempt to refresh the existing session.
80
+
session = await client.sessionRefresh(session);
81
+
} catch (e) {
82
+
// Couldn't refresh the session so reauthenticate.
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.
91
+
92
+
All requests are sent with a session object which authorizes the client.
93
+
94
+
```dart
95
+
final account = await client.getAccount(session);
96
+
final username = account.user.username;
97
+
final avatarUrl = account.user.avatarUrl;
98
+
final userId = account.user.id;
99
+
```
100
+
101
+
### Socket
102
+
103
+
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.
104
+
105
+
```dart
106
+
NakamaWebsocketClient.init(
107
+
host: '127.0.0.1',
108
+
ssl: false,
109
+
token: _session.token,
110
+
);
111
+
```
112
+
113
+
Remember to close the connection after disposing of the app widget:
0 commit comments