NakamaClient 2.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NakamaClient --version 2.2.0
NuGet\Install-Package NakamaClient -Version 2.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NakamaClient" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NakamaClient --version 2.2.0
#r "nuget: NakamaClient, 2.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NakamaClient as a Cake Addin
#addin nuget:?package=NakamaClient&version=2.2.0

// Install NakamaClient as a Cake Tool
#tool nuget:?package=NakamaClient&version=2.2.0

Nakama .NET

.NET client for Nakama server written in C#.

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 client implements the full API and socket options with the server. It's written in C# with minimal dependencies to support Unity, Xamarin, Godot, XNA, and other engines and frameworks.

Full documentation is online - https://heroiclabs.com/docs

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.

  1. Install and run the servers. Follow these instructions.

  2. Download the client from the releases page and import it into your project. You can also build from source.

  3. Use the connection credentials to build a client object.

    // using Nakama;
    const string scheme = "http";
    const string host = "127.0.0.1";
    const int port = 7350;
    const string serverKey = "defaultkey";
    var client = new Client(scheme, host, port, serverKey);
    

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.

var email = "super@heroes.com";
var password = "batsignal";
var session = await client.AuthenticateEmailAsync(email, password);
System.Console.WriteLine(session);

Sessions

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

System.Console.WriteLine(session.AuthToken); // raw JWT token
System.Console.WriteLine(session.UserId);
System.Console.WriteLine(session.Username);
System.Console.WriteLine("Session has expired: {0}", session.IsExpired);
System.Console.WriteLine("Session expires at: {0}", session.ExpireTime);

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.

var authtoken = "restored from somewhere";
var session = Session.Restore(authtoken);
if (session.IsExpired)
{
    System.Console.WriteLine("Session has expired. Must reauthenticate!");
}

NOTE: The length of the lifetime of a session can be changed on the server with the "--session.token_expiry_sec" command flag argument.

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 in 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.

var account = await client.GetAccountAsync(session);
System.Console.WriteLine(account.User.Id);
System.Console.WriteLine(account.User.Username);
System.Console.WriteLine(account.Wallet);

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.

var socket = Socket.From(client);
socket.Connected += () =>
{
    System.Console.WriteLine("Socket connected.");
};
socket.Closed += () =>
{
    System.Console.WriteLine("Socket closed.");
};
socket.ReceivedError += e => System.Console.WriteLine(e);
await socket.ConnectAsync(session);

Contribute

The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested to improve the code please open an issue to discuss the changes or drop in and discuss it in the community chat.

Source Builds

The codebase can be built with the Dotnet CLI. All dependencies are downloaded at build time with Nuget.

dotnet build src/Nakama/Nakama.csproj

For release builds use:

dotnet build -c Release /p:AssemblyVersion=2.0.0.0 src/Nakama/Nakama.csproj

Run Tests

To run tests you will need to run the server and database. Most tests are written as integration tests which execute against the server. A quick approach we use with our test workflow is to use the Docker compose file described in the documentation.

docker-compose -f ./docker-compose-postgres.yml up
dotnet test tests/Nakama.Tests/Nakama.Tests.csproj

License

This project is licensed under the Apache-2 License.

Special Thanks

Thanks to Alex Parker (@zanders3) for the excellent json library and David Haig (@ninjasource) for Ninja.WebSockets.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.12.0 106 4/8/2024
3.11.0 165 3/8/2024
3.10.0 642 11/21/2023
3.9.0 649 8/11/2023
3.8.0 621 6/9/2023
3.7.0 749 3/10/2023
3.6.0 929 2/7/2023
3.5.0 944 9/18/2022
3.4.0 975 4/28/2022
3.3.0 821 1/24/2022
3.2.0 1,115 10/11/2021
3.1.1 708 8/19/2021
3.1.0 712 8/11/2021
3.0.0 732 7/14/2021
2.9.3 733 6/17/2021
2.9.2 770 5/21/2021
2.9.1 703 5/19/2021
2.9.0 793 5/15/2021
2.8.0 5,349 2/19/2021
2.7.1 770 2/18/2021
2.7.0 3,986 10/19/2020
2.6.0 786 9/21/2020
2.5.0 1,098 8/12/2020
2.4.0 1,127 5/4/2020
2.3.1 2,900 9/21/2019
2.3.0 928 9/2/2019
2.2.2 928 7/1/2019
2.2.1 2,490 6/19/2019
2.2.0 898 6/17/2019