Skip to content

Commit 6e70de1

Browse files
committed
Merge branch 'main' into develop
2 parents dd3377a + d135997 commit 6e70de1

File tree

1 file changed

+93
-14
lines changed

1 file changed

+93
-14
lines changed

README.md

+93-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
# Nakama Flutter Client 🤩 🎮 🌍 🛰
1+
# Nakama Dart/Flutter Client
22

3-
[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).
44

5-
![GitHub issues](https://img.shields.io/github/issues-raw/obrunsmann/flutter_nakama?style=flat-square)
6-
![GitHub last commit](https://img.shields.io/github/last-commit/obrunsmann/flutter_nakama?style=flat-square)
5+
This is a Flutter client for Nakama written in pure Dart and supports cross platform gaming on iOS, Android, Web and more.
6+
7+
![GitHub issues](https://img.shields.io/github/issues-raw/heroiclabs/nakama-dart?style=flat-square)
8+
![GitHub last commit](https://img.shields.io/github/last-commit/heroiclabs/nakama-dart?style=flat-square)
79
![Pub Version](https://img.shields.io/pub/v/nakama?style=flat-square)
810

911
---
1012
## 🚀 Getting Started
1113

12-
### 1. Setup Nakama Server
13-
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`:
1421

15-
### 2. Add flutter\_nakama pre-release to pubspec.yaml.
1622
```yaml
1723
name: your_game
1824
dependencies:
@@ -21,7 +27,8 @@ dependencies:
2127
nakama: ^1.0.0-dev.6
2228
```
2329
24-
### 3. Create nakama base client
30+
3. Use the connection credentials to build a client object:
31+
2532
```dart
2633
final client = getNakamaClient(
2734
host: '127.0.0.1',
@@ -31,8 +38,15 @@ final client = getNakamaClient(
3138
httpPort: 7350, // optional
3239
);
3340
```
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+
3650
```dart
3751
final session = await getNakamaClient().authenticateEmail(
3852
@@ -42,9 +56,74 @@ final session = await getNakamaClient().authenticateEmail(
4256
print('Hey, you are logged in! UserID: ${session.userId}');
4357
```
4458

45-
# Documentation
46-
**Flutter SDK Docs:**
47-
[https://flutter-nakama.gitbook.io](https://flutter-nakama.gitbook.io)
59+
### Sessions
60+
61+
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.
83+
session = await client.authenticateDevice(deviceId: deviceId);
84+
}
85+
}
86+
```
87+
88+
### Requests
89+
90+
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:
114+
115+
```dart
116+
NakamaWebsocketClient.instance.close();
117+
```
118+
119+
## Documentation
120+
121+
**Dart/Flutter SDK Docs:**
122+
[https://heroiclabs.com/docs/nakama/client-libraries/dart/](https://heroiclabs.com/docs/nakama/client-libraries/dart/)
48123

49124
**Nakama Docs:**
50-
[https://heroiclabs.com/docs](https://heroiclabs.com/docs)
125+
[https://heroiclabs.com/docs/nakama/](https://heroiclabs.com/docs/nakama)
126+
127+
# Special Thanks
128+
129+
Thanks to Oliver Brunsmann (@obrunsmann) for his excellent contribution and maintenance of this library.

0 commit comments

Comments
 (0)