Unity SDK Client JoinPartyAsync

Hi there,

I am trying to use the Party feature of Nakama with the Unity SDK.

I have code to send a party invite, which looks like this:

private async void InviteFriendToParty()
		{
			IParty party = _connection.CurrentParty;
			try
			{
				if (party == null) {
					party = await _connection.Socket.CreatePartyAsync(false, 4);
					_connection.SetParty(party);
				}
				Debug.Log($"Party: {_connection.CurrentParty.Id}");
			}
			catch (Exception e) 
			{
				Debug.LogError("Inviting friend failed (" + e.Message + ")");
			}
			
			try
			{
				var payload = new Dictionary<string, string>
				{
					{ "username", _connection.Account.User.Username },
					{ "friendId", _friend.User.Id },
					{ "partyId", party?.Id }
				};
				var response = 
					await _connection.Client
						.RpcAsync(_connection.Session, "inviteFriendToParty", payload.ToJson());
				Debug.Log("Party invite send successfully");
			}
			catch (ApiResponseException ex)
			{
				Debug.LogFormat("Error: {0}", ex.Message);
			}
		}

On the server, I have an RPC function that sends the Party Invite to the Friend as a notification, which looks like this:

const inviteFriendToParty: nkruntime.RpcFunction =
    function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string) {
        const content = JSON.parse(payload);
        const inviterUserName = content.username;
        const inviteeUserId = content.friendId;
        const notification: nkruntime.NotificationRequest = {
            code: 4,
            content: content,
            persistent: false,
            subject: `Join ${inviterUserName}'s party!`,
            userId: inviteeUserId,
        }
        nk.notificationsSend([notification]);
    }

This triggers and fires off the Notification to the Friend perfectly, and the Friend receives the Party Invite.

In my notification to the Friend, they get a button that says “Join Party”, which executes this code:

private async void JoinPartyInvite(string partyId)
		{
			try
			{
				await _connection.Socket.JoinPartyAsync(partyId);
			}
			catch (Exception e)
			{
				Debug.LogError("Joining party failed (" + e.Message + ")");
			}
		}

and I have a listener on my client’s Socket that listens for the ReceivedParty event from Nakama, which looks like this:

_socket.ReceivedParty += party =>
			{
				Debug.Log("Joined party: " + party);
			};

However, nothing happens.
No error message, no Debug.Log message from above.
Nothing.

I did notice in my server logs, that this pops up when I attempt to accept the Party invite:

{"level":"debug","ts":"2022-04-16T04:29:59.064Z","caller":"server/pipeline.go:65","msg":"Received *rtapi.Envelope_PartyJoin message","uid":"c6f4e975-b0c0-430f-9b78-1f28517f42a6","sid":"c3a7c69b-bd3d-11ec-8748-006100a0eb06","cid":"0","message":{"PartyJoin":{"party_id":"4d1ed84b-9064-44cd-ac1b-8c5c5de0f1b0.nakama"}}}

{"level":"debug","ts":"2022-04-16T04:29:59.064Z","caller":"server/session_ws.go:395","msg":"Sending <nil> message","uid":"c6f4e975-b0c0-430f-9b78-1f28517f42a6","sid":"c3a7c69b-bd3d-11ec-8748-006100a0eb06","envelope":"cid:\"0\""}

It is strange to me that it says “Sending <nil> message”.

What am I doing wrong?

Any and all help is appreciated.

Thank you :slight_smile:

  1. Versions: Nakama {3.11.0}, {Docker}, {Unity (SDK) 3.3.0}
  2. Server Framework Runtime language (If relevant) {TS/JS}

Hey @lightning thanks for the thorough post.

When a leader creates a closed party, it’s not enough for the leader to just send the party ID to the user and wait for them to join. The leader must also listen for a ReceivedPartyJoinRequest and call AcceptPartyMemberAsync upon receiving it. This extra step is included for a variety of reasons, including improving user experience and security.

You can look at the following test as an example of how to add a user to a closed group: