Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

SignalR Core-Send Message to Group Outside of Hub

$
0
0

I have an ASP.NET Core 8 Web API with a SignalR endpoint:

builder.Services.AddSignalR(hubOptions =>{    hubOptions.EnableDetailedErrors = true;}).AddJsonProtocol(jsonOptions => { jsonOptions.PayloadSerializerOptions.PropertyNamingPolicy = null; });app.MapHub<ChatHub>("/chatHub", options =>{});

I have a Blazor client inside OnInitializedAsync:

HubConnection = new HubConnectionBuilder()    .WithUrl(Navigation.ToAbsoluteUri("https://localhost:7000/chatHub"), options =>    {                    options.Headers.Add("BusinessId", businessId.ToString());    }).AddJsonProtocol(o =>    {        o.PayloadSerializerOptions.PropertyNamingPolicy = null;    }).WithAutomaticReconnect()    .Build();HubConnection.On<ChatMessageDTO>("ReceiveMessage", async (msg) =>{    Model?.ChatMessages?.Add(msg);    var messageId = await ChatService.AddNewMessage(msg, Cts.Token);    await InvokeAsync(StateHasChanged);});await HubConnection.StartAsync(Cts.Token);

Everything is working correctly. I can send and receive messages from my SignalR beacon to my client callbacks. However, If I try to send a group message to a Blazor client callback from outside of the hub, I get inconsistent results if any.

Here's my API service:

public class FcmService : IFcmService{    private readonly IHubContext<ChatHub> _hub { get; }    public FcmService(IHubContext<ChatHub> hub)    {       _hub = hub;    }    public async Task UpdateUi(Message message, CancellationToken token)    {        var group = string.Format("{0}{1}", message.BusinessId, "-Dispatchers");        await _hub.Clients.Group(group).SendAsync("ReceiveMessage", message);    }}

When I send the message to a group, the Blazor client callback never gets hit. If I change this line:

await _hub.Clients.Group(group).SendAsync("ReceiveMessage", message);

to:

await _hub.Clients.All.SendAsync("ReceiveMessage", message);

it works every time.

How do I send a message to a group from outside of the hub?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>