question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OnConnectedAsync missing some of the connections

See original GitHub issue

I’m creating 10 clients in a loop. Almost all the time, a few of these clients don’t trigger the OnConnectedAsync method when they connect to the hub. These clients don’t throw any exceptions when they start a connection using the connection.StartAsync() or when the connection.On handler is registered. Which makes it hard to diagnose any problems.

Fiddler traffic displays 10 client/negotiate, 10 client/?hub, 10 /message/negotiate requests. But I see 9 OnConnectedAsync calls in the console.

This is the fiddler traffic:

image

This is the Hub’s OnConnectedAsync method:

public override async Task OnConnectedAsync()
{
	Console.WriteLine("@@@ OnConnectedAsync is executed");
	if (Context?.User?.Identity != null)
	{
		var playerId = ((System.Security.Claims.ClaimsIdentity)Context.User.Identity).Claims
			.First(p => p.Type == "PlayerId")
			.Value;

		//some logic
	}

	await base.OnConnectedAsync().ConfigureAwait(false);
}

This is the client code:

for (var i = 1; i <= 10; i++)
{
		var token = await program.GetToken(i);
		await program.CreateClient(i, token);
}

private async Task CreateClient(int playerId, string token)
{
	var connection = new HubConnectionBuilder()
		.WithUrl(_configurationRoot["SignalRHubUrl"],
			options => options.Headers = new Dictionary<string, string>
			{
					{
						"Authorization", $"bearer {token}"
					}
			})
		.Build();

	await connection.StartAsync();

	connection.On<string>("send", async (m) =>
	{
		Console.WriteLine($"Message received: {m}");
		await connection.StopAsync();
	});

	Console.WriteLine($"Player {playerId} connected.");
}

These are the console logs that display OnConnectedAsync’s:

Now listening on: http://localhost:51234 Application started. Press Ctrl+C to shut down. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3] HttpConnection Started. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 264.7313ms 200 application/json info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 8.4074ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7.6316ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 13.7569ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 8.4681ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.1647ms 200 application/json info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.8534ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.5252ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7.1773ms 200 application/json info@@@ OnConnectedAsync is executed : Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://localhost:51234/message/negotiate 0 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] Successfully validated the token. info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Authorization was successful. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 6.7461ms 200 application/json info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. @@@ OnConnectedAsync is executed

ConnectionId of player 2 is missing:

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
xscriptcommented, Jun 8, 2018

@yigitgursoy We found the root cause of this issue. It is an edge case and only happens on Free tier. We have fixed it and will update here once it is deployed.

1reaction
davidfowlcommented, Jun 2, 2018

Unrelated:

You should be using the access token provider to set the bearer token.

var connection = new HubConnectionBuilder()
		.WithUrl(_configurationRoot["SignalRHubUrl"],
			options => options.AccessTokenProvider = () => Task.FromResult<string>(token))
		.Build();

You don’t need to cast this:

var playerId = ((System.Security.Claims.ClaimsIdentity)Context.User.Identity).Claims
			.First(p => p.Type == "PlayerId")
			.Value;

It’s already a ClaimsIdentity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OnConnectedAsync not firing all the time with Azure ...
However, when the connection is successfully made, the OnConnectedAsync method on the backend hub is only firing about 10% of the time.
Read more >
Use hubs in SignalR for ASP.NET Core
The SignalR Hubs API provides the OnConnectedAsync and OnDisconnectedAsync virtual methods to manage and track connections.
Read more >
SignalR Troubleshooting
The solution is to stop the SignalR connection before logging the client out. "Uncaught Error: SignalR: jQuery not found. Please ensure jQuery ...
Read more >
Managing SignalR ConnectionIds (or why you shouldn't)
If you're running multiple versions of your server at any moment, I'm going to assume you're using Redis backplane or Azure SignalR Server....
Read more >
Build Real-time Applications with ASP.NET Core SignalR
Clients attempted to reconnect if a connection was lost, and the server buffered unsent messages and replayed them when a client reconnected ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found