question

bkiefer1992 avatar image
bkiefer1992 asked

How do I connect a SignalR client to PlayFab to get realtime lobby changes?

Firstly, I'm unable to use The Multiplayer SDK because I'm using Unity WebGL (as per this answered question).

I'm attempting to get realtime lobby updates. I can't use the GetLobby REST API because it seems to throttle my calls at around a call every eight seconds.

I'm opting to use Realtime notifications, as advised in the answer to this question.

It's my understanding that I must use SignalR to use PlayFab's realtime notifications. I have successfully included the ASP.NET Core SignalR .Net Client in my Unity project using this guide. I understand I'll probably need to use javascript for WebGL, but I'm getting things set up in the Unity editor first using the C Sharp version.

I'm attempting to follow this documentation to connect to the SignalR hub. I'm creating a HubConnection in C Sharp like this:

string url = $"https://9EBFC.playfabapi.com/pubsub";
m_connection = new HubConnectionBuilder().WithUrl(url).Build();

'9EBFC' is our title ID. The following exception is thrown by the connection upon calling m_connection.StartAsync:

HttpRequestException: 401 (Unauthorized)

Am I correct in assuming the SignalR negotiate REST API must be passed in as the URL of my SignalR connection, as I have done in my code snippet? Am I correct in assuming 'titleId' part of the URL in the previous API documentation must be replaced with my title's title ID? I get the same error if I instead use the literal string 'https://titleId.playfabapi.com/PubSub/Negotiate'.

The Real-time notifications SignalR Hub documentation previously linked mentions the auth headers required to call the API can be passed to the SignalR client via HttpConnectionOptions. How, specifically, might I do that? I know I must use this overload of the WithURL method, but what should I add during the HttpConnectionOptions callback? Are there any examples? I have looked at the PlayFab messaging sample, which isn't quite the same thing, but it does use that overload. It seems to pass an access token it retrieves from a CloudScript call. Must I use Cloudscript, or is that not relevant for getting realtime lobby updates?

multiplayer
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

To create the connection to the SignalR Hub, you need the AccessToken and Url which can be obtained with Pub Sub - Negotiate - REST API (PlayFab Multiplayer) | Microsoft Learn. Method WithUrl has multiple overloads, you may need this one to set both AccessToken and Url.

7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bkiefer1992 avatar image bkiefer1992 commented ·

Hey Gosen,

Okay, that makes sense. Thanks for clearing that up.

I can't seem to find any equivalent negotiate method in the PlayFab SDK for Unity. Is there an additional package I need to download apart from the PlayFab SDK for Unity? I tried my luck at calling the pubsub/negotiate API using a Unity web request (with an X-EntityKey I retrieved from logging in with a custom ID) but got the same 401 unauthorized error.

I also tested calling another API (CancelMatchmakingTicket) and I get a 400 Bad Request error (as expected), which indicates to me that something different is happening in the case of negotiate and I'm not just sending the wrong headers.

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao bkiefer1992 commented ·

The negotiate method is not in the SDK, you need to send the request with HTTP request. May I know the 401 error is returned by negotiate method or Hub Connection method? Can you share us the code snippet for research? Please remove any sensitive info.

0 Likes 0 ·
bkiefer1992 avatar image bkiefer1992 Gosen Gao commented ·

Okay then.

The 401 error was returned by the web request that called the PubSub/Negotiate API.

Here is a code snippet I created which reproduces the 401 error.
playfabnegotiatetest.cs.txt

0 Likes 0 ·
Show more comments
bkiefer1992 avatar image bkiefer1992 commented ·

You absolute wizard Gosen!

Your code worked; the web request returned an access token and a URL . Once again, thank you for very much for your assistance.

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao bkiefer1992 commented ·

After doing more tests, I just realize that you don't need to call Negotiate manually. Although it works with code:

connection = new HubConnectionBuilder() 
.WithUrl("Url-From-Negotiate-API", options =>
{
    options.AccessTokenProvider = async () =>
    {
        return "AccessToken-From-Negotiate-API";
    };
})
.WithAutomaticReconnect()
.Build();

But according to our docs, it should be the code below. Sorry for the confusion.

connection = new HubConnectionBuilder() 
.WithUrl("https://xxxxx.playfabapi.com/PubSub", options =>
{
    options.Headers.Add("X-EntityToken", token);
})
.WithAutomaticReconnect()
.Build();
0 Likes 0 ·
bkiefer1992 avatar image bkiefer1992 Gosen Gao commented ·

Thanks for the additional info.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.