This guide details the integration process between Nakama’s powerful social matchmaking system and Edgegap’s session management, enabling developers to efficiently scale multiplayer games.
Prerequisites #
Before proceeding ensure that you have:
- Created an Edgegap Account
- Installed Nakama server
- Installed the Nakama Unity SDK
Creating the Headless Unity server #
This guide covers just the required steps to get your headless Unity server configured to work with Nakama and Edgegap. It does not cover synchronizing game state between the headless server and the client, for this please see the documentation for your chosen networking framework (e.g. Nakama, Unity Netcode for GameObjects, Mirror, etc).
Installing the Nakama SDK in Unity #
To install the Nakama SDK into the Unity project:
- Download the latest Nakama Unity SDK package.
- Double-click the
Nakama.unitypackage
and install it in your Unity project.
Installing the Edgegap Server Nakama Plugin #
- Open your Unity project,
- Select toolbar option Window -> Package Manager,
- Click the + icon and select Add package from git URL…,
- Input the following URL
https://github.com/edgegap/edgegap-server-nakama-plugin-unity.git
, - Click Add and wait for the Unity Package Manager to complete the installation.
Import Simple Example #
- Find this package in Unity Package Manager window.
- Open the
Samples
tab. - Click on Import next to Simple Handler Example.
- Locate sample files in your project
Assets/Samples/Edgegap Server Nakama Plugin/{version}/Simple Handler Example
. - Create an Empty GameObject in your scene and attach
SimpleHandlerExample.cs
script.
This script will call the methods AddUser
and RemoveUser
whenever a player connects or disconnects to manage allocations on Nakama.
After these steps are done, your project is ready to be launched on Edgegap, for that go to Tools -> Edgegap Hosting.
Login and follow steps 1-5, since we are doing automatic deployment via Nakama, step 6 can be skipped.
Now follow the next section to setup the Nakama Server and integrate it with Edgegap to deploy servers automatically.
Troubleshooting #
Visual Studio shows
type or namespace name could not be found
for Edgegap namespace.
- In your Unity Editor, navigate to Edit / Preferences / External Tools / Generate .csproj files.
- Make sure you have enabled Git packages.
- Click Regenerate project files.
Nakama Initialization #
To add the Edgegap Fleet Manager to your Nakama go plugin project, use the following command:
|
|
This will add it to your project’s go.mod
dependencies.
Nakama Setup #
You must set up the following Environment Variables inside your Nakama’s cluster:
|
|
You can copy the local.yml.example
to local.yml
and fill it out to start with your local cluster
Make sure the NAKAMA_ACCESS_URL
is prefixed with https://
.
Optional Values with default
|
|
Using the Nakama’s Storage Index and basic struct Instance Info, we store extra information in the metadata for Edgegap using 2 list. 1 list to holds seats reservations 1 list to holds active connections
Using Max Players field we can now create the field AvailableSeats
that will be in sync with that (
MaxPlayers-Reservations-Connections=AvailableSeats)
Usage #
From your main.go
where the InitModule
global function is, you need to register the Fleet Manager
|
|
Run the following command to start a local cluster:
|
|
Run the following command to stop it
|
|
Server Placement #
Game clients only interact with Edgegap APIs through Nakama RPCs, defaulting to Nakama authentication method of your choice. Edgegap’s Server Placement utilizing Server Score strategy uses public IP addresses of participating players to choose the optimal server location. To store the player IP address and pass it to Edgegap when looking for server, store player’s public IP in their Profile’s Metadata as PlayerIP
.
In your main.go
, during the Init you can add the Registration of the Authentication of the type you implemented
|
|
This will automatically store in Profile’s Metadata the PlayerIP
Dedicated Game Server -> Nakama Instance #
When using this integration, every Deployment (Dedicated Game Server) made through Edgegap’s platform will have many Environment Variables injected.
It’s the responsibility of the Dedicated Game Server to fire lifecycle events when specific actions are triggered to communicate back to Nakama’s cluster changes regarding the Instance (Nakama’s reference to an Edgegap Deployment) and facilitate Player connections to the Dedicated Game Server.
Unity Server Plugin #
Automate all server responsibilities (instance and connection event reporting) by using our Edgegap Server Nakama Plugin for Unity.
Injected Environment Variables #
The following Environment Variables will be available in the Dedicated Game Server:
NAKAMA_CONNECTION_EVENT_URL
(url to send connection events of the players)NAKAMA_INSTANCE_EVENT_URL
(url to send instance event actions)NAKAMA_INSTANCE_METADATA
(contains create metadata JSON)
Connection Events #
Using NAKAMA_CONNECTION_EVENT_URL
you must send Player Connection events to the Nakama Instance with the following body:
|
|
connections
is the list of active user IDs connected to the Dedicated Game Server. We recommend collecting updates
over a short period of time (~5 seconds) and updating the full list of connections in a batch request. Contents of
this request will overwrite any existing list of connections for the specified instance.
Instance Events #
Using NAKAMA_INSTANCE_EVENT_URL
you must send Instance events to the Nakama Instance with the following body:
|
|
action
must be one of the following:
READY
will mark the instance as ready and trigger Nakama callback event to notify players,ERROR
will mark the instance in error and trigger Nakama callback event to notify players,STOP
will call Edgegap’s API to stop the running deployment, which will be removed from Nakama once Edgegap confirms termination.
message
can be used optionally to provide extra Instance status information (e.g. to communicate Errors).
metadata
can be used optionally to merge additional custom key-value information available in Dedicated Game Server to the metadata of the Instance.
Game Client -> Nakama (optional rpc) #
We included a Client RPC route to do basic operations on Instance - listing, creating, and joining. Consider this an optional starter code sample. For production/live use cases, we recommend using a matchmaker for added security and flexibility.
Create Instance #
RPC - instance_create
|
|
max_players
to -1 for unlimited. Use with caution, we recommend performing a benchmark for server resource usage impact.
If user_ids
is empty, the requesting user’s ID will be used.
Get Instance #
RPC - instance_get
|
|
List Instance #
RPC - instance_list
|
|
query
can be used to search instance with available seats.
Example to list all instances READY with at least 1 seat available.
|
|
Join Instance #
RPC - instance_join
|
|
If user_ids
is empty, the requesting user’s ID will be used.
Matchmaker #
You can create your own integration using Nakama’s Matchmaker, see our starter code sample:
|
|
Register OnMatchmakerMatched
callback like this in the InitModule
function in your main.go
:
|
|