Edgegap-Nakama Callback Fix¶
Problem¶
The Nakama server was successfully creating Edgegap instances, but the callback was never being executed, which meant:
- Players never received connection info notifications
- The server never got the instance metadata
- NAKAMA_INSTANCE_METADATA environment variable was "null"
Root Cause¶
The dedicated server was trying to send the READY event in TankGameState::BeginPlay() before it had the instance_id. The instance_id is required to send events back to Nakama, but it wasn't available yet because:
1. Edgegap doesn't inject NAKAMA_INSTANCE_METADATA properly (shows as "null")
2. The instance_id comes from clients via URL parameter ?instance_id=xxx
3. BeginPlay() happens before any client connects
Solution¶
Changed the flow so the READY event is sent after the first client connects and provides the instance_id:
Modified Files¶
1. EdgegapInstanceReporter.h¶
- Added
HasInstanceId()method to check if instance ID is set - Added
bReadyEventSentstatic flag to prevent duplicate READY events
2. EdgegapInstanceReporter.cpp¶
- Modified
SetInstanceId()to automatically send READY event when instance_id is set - Added validation to prevent changing instance_id once set
- Added comprehensive logging for debugging
3. TankGameState.cpp¶
- Removed READY event sending from
BeginPlay() - Added logging to indicate server is waiting for first player connection
4. TankGameMode.cpp¶
- Enhanced logging in
InitGame()to track instance_id setting
New Flow¶
1. Nakama matchmaker matches players
2. Nakama calls efm.Create() to spin up Edgegap instance
3. Edgegap creates VM and starts dedicated server
4. Server starts, but does NOT send READY event yet
5. Nakama callback (CreateSuccess) sends notification to clients with InstanceId
6. Client receives notification with connection info + InstanceId
7. Client connects to server: "server.com:7777?instance_id=abc123"
8. Server's InitGame() parses instance_id from URL
9. SetInstanceId() is called with the instance_id
10. SetInstanceId() automatically sends READY event to Nakama
11. Nakama receives READY event and triggers callback
12. Callback sends connection-info notification to all players
13. Players can now connect to the server
Testing¶
After deploying these changes: 1. Check server logs for: "Instance ID set to: [id]" 2. Check server logs for: "READY event sent for instance: [id]" 3. Check Nakama logs for the callback being triggered 4. Verify clients receive connection-info notification
Important Notes¶
- The instance_id MUST be passed as a URL parameter by clients
- This is already implemented in
NakamaConnectionSubsystem.cppline 204-208 - Local testing won't have instance_id - this is expected and logged as a warning
- On Edgegap deployments, if instance_id is missing, something is wrong with the client connection flow