I'm using Azure SignalR Services, which is running in the cloud, and running an Azure Function App, which runs locally on my laptop on localhost. I have the following hub:
public class LeaderboardHub : ServerlessHub
{
[FunctionName("negotiate")]
public async Task<SignalRConnectionInfo> Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req) =>
Negotiate(req.Headers["x-ms-signalr-user-id"]);
[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger]InvocationContext invocationContext, ILogger logger)
{
logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}
}
I connect to my hub like so:
const apiBaseUrl = window.location.origin;
const connection = new signalR.HubConnectionBuilder()
.withUrl(apiBaseUrl + '/api')
.configureLogging(signalR.LogLevel.Information)
.build();
connection.start()
.catch(console.error);
When I run this, I can set a breakpoint at Negotiate()
and see the connection made. When I send messages to my hub, they show up on the client. However, I never see the OnConnected
method get called. Likewise, if I create new SignalRTrigger
methods and call them in Javascript, they never get called.
My Theory: I'm hoping I'm wrong, but I'm somewhat guessing this is because I'm running my app on my own laptop under localhost, and Azure SignalR Services has no way to "forward" the message on to me, since Azure can't connect to my laptop. I haven't tried deploying my Function App to Azure to run it there, but I'm guessing if I ran it within Azure, there would be a two-way connection.
Thanks!
Figured out my own question! First off, I was right. Second, there is a very cool SignalR local emulator that can be found here:
https://github.com/Azure/azure-signalr/blob/dev/docs/emulator.md
After installing and running this, my code worked perfectly as expected.