Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@aspnet/signalr - Unable to receive a message sent to concrete client #17106

Closed
StanislavChankov opened this issue Nov 14, 2019 · 13 comments
Closed
Labels
area-signalr Includes: SignalR clients and servers
Milestone

Comments

@StanislavChankov
Copy link

StanislavChankov commented Nov 14, 2019

Describe the bug

I am unable to receive a message sent from AspNetCore WebApi via Microsoft.AspNetCore.SignalR Hub to concretely targeted client.
The issue I am encountering exists only, when I am trying to send the message to single client:
await Clients.Client("Valid connection Id").SendAsync("Client message", response);

I am successfully sending the same message passing the same flow, when sending it to all of the clients:
await Clients.All.SendAsync("Client message", response);

To Reproduce

The code pictures are just examples of what I am trying to do.

Back-end AspNetCore side
image

Front-end Angular side
image

Further technical details

  • ASP.NET Core version - netcoreapp3.0
  • Node.js version - 10.16.3
  • Angular version - 8.3.6
  • aspnet/signalr version - 1.1.4
  • Include the output of dotnet --info
    image

image

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Microsoft Visual Studio Community 2019 - 16.3.5
    image

image

@javiercn javiercn added the area-signalr Includes: SignalR clients and servers label Nov 14, 2019
@BrennanConroy
Copy link
Member

First question I have to ask, how do you know you have a valid connection Id?

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 14, 2019

First question I have to ask, how do you know you have a valid connection Id?

Good question I could say. I am caching the connections to the memory cache and also logging the disconnected users via override Task OnDisconnectedAsync(Exception exception) method.

@BrennanConroy
Copy link
Member

And you're using Context.ConnectionId from inside the Hub to get the connection ID initially?

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 14, 2019

And you're using Context.ConnectionId from inside the Hub to get the connection ID initially?

Exactly.

@BrennanConroy
Copy link
Member

I can't think of anything that would be wrong from the info provided. Would it be possible for you to make a minimal repro app and put it on github?

@halter73
Copy link
Member

halter73 commented Nov 14, 2019

If you send a message directly to Context.ConnectionId in OnConnectedAsync, does that work?

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 17, 2019

If you send a message directly to Context.ConnectionId in OnConnectedAsync, does that work?

@halter73, No. I am not able to send any message to anyone from OnConnectedAsync. I have tried Client(Context.ConnectionId), All, Caller, but isn't it expected behavior? I am not able to send any message from OnConnectedAsync in other hubs as well. I tried in other method sending a message to the same connection
await Clients.Client(Context.ConnectionId).SendAsync("method", new {});, but doesn't receive it.

I wrote an integration test, which shows exactly the same behavior. I am able to send a message, when I target all of the clients, but not able, when I select exact client. Therefore we are eliminating the possible issues in my client application and @aspnet/signalr library.

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 17, 2019

I can't think of anything that would be wrong from the info provided. Would it be possible for you to make a minimal repro app and put it on github?

@BrennanConroy, Is there a small app with netcoreapp3.0 with working example showing how to send private messages Clients.Client()

@BrennanConroy
Copy link
Member

Sure, I created a super simple 3.0 app that sends to Clients.Client(Context.ConnectionId) in OnConnectedAsync.

https://github.com/BrennanConroy/3_0_Sample

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 19, 2019

Good job @BrennanConroy ! I have tested it and it is working perfectly. I created another branch in your repo, where I cached in the memory the connections in OnConnectedAsync and send the message to the other user in SendMessage and it is working properly.

I noticed that you are using @microsoft/signalr, which is created for AspNetCore. However I am using @aspnet/signalr as I have described above. I guess that's my mistake and switching to @microsoft/signalr@3.0.0 will fix the issue. One more thing, not only sending to concrete client, but also OnDisconnectedAsync is not working on @aspnet/signalr@1.1.4 with netcoreapp3.0

@BrennanConroy
Copy link
Member

However I am using @aspnet/signalr as I have described above. I guess that's my mistake and switching to @microsoft/signalr@3.0.0 will fix the issue.

Using @aspnet/signalr shouldn't cause any issues. I switched the sample to @aspnet/signalr@1.1.4 and it continued working as expected.

One more thing, not only sending to concrete client, but also OnDisconnectedAsync is not working on @aspnet/signalr@1.1.4 with netcoreapp3.0

What isn't working? You probably wont be able to send a message to the specific client that is closing because it is in the process of shutting down.

Could you please provide a sample app that shows these things not working properly?

@StanislavChankov
Copy link
Author

StanislavChankov commented Nov 19, 2019

  1. I had an issue with authorization. Exactly the same as ddydeveloper had in Authorize attribute doesn't work with SignalR ASP.Net Core 2.1 aspnet/SignalR#1764 . Hope that help to anyone.
            services
                .AddAuthentication("Bearer") // Just an example
                .AddJwtBearer(options =>
                {
                    options.Events = new JwtBearerEvents
                    {
                        OnMessageReceived = context =>
                        {
                            var accessToken = context.Request.Query["access_token"];
                            if (!string.IsNullOrWhiteSpace(accessToken))
                            {
                                context.Token = accessToken;
                            }

                            return Task.CompletedTask;
                        }
                    };
                });
  1. I worked around the issue with Clients.Client(clientConnectionId) with Clients.User(staOpsConnections.Id). I guess I will come back, when I have to create a anonymous hub. I am sorry that I don't have enough time to provide you a sample repo of the issue.

@BrennanConroy , thank you one more time for taking the time to help me.

@analogrelay analogrelay added this to the Discussions milestone Nov 20, 2019
@ghost
Copy link

ghost commented Nov 12, 2020

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

@ghost ghost closed this as completed Nov 12, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

No branches or pull requests

5 participants