Zamboni
Zamboni

Reputation: 8043

SignalR cannot send a message to all connected clients

I am trying to using SignalR to send a message to all connected clients.

I have found several examples and added what I think to be all the required bits. I did successfully get my client to connect to my Hub. I cannot get my server to connect to my Hub and send a message to all the connected clients.

When I call DatabaseChangeListener::Notify() it never hits the code in the Hub.

Can anyone suggest what else I need to do?

I am using .NET Core 2.1 preview 2 in web application with React and Redux.

I am using SignalR 1.0.0-preview2-final

I am using SignalR.Client 1.0.0-preview2-final

In Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
      // remove all other code for this question
      services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app)
    {
      // remove all other code for this question
      app.UseSignalR(routes =>
      {
        routes.MapHub<SignalRHub>("/hubs/update");
      });
    }

My Hub

    [Authorize]
    public class SignalRHub : Hub
    {
        public async Task Send(string message)
        {
            await Clients.All.SendAsync("SendMessage", Context.User.Identity.Name, message);
        }   
    }

My class to notify clients

public class DatabaseChangeListener : IDatabaseChangeListener
{
    private readonly IHubContext<SignalRHub> _hubContext;

    public DatabaseChangeListener(IHubContext<SignalRHub> hubContext)
    {
            _hubContext = hubContext;

    }

    public void Notify()
    {
      _hubContext.Clients.All.SendAsync("SendMessage", "something changed, Yo");
    }
}

Upvotes: 3

Views: 1400

Answers (1)

Mike
Mike

Reputation: 16

You need to make the connection to the hub via client side and then by using your _hubContext, you should be able to send a message to the client based off of the connection made to the hub.

Connection to the hub from client side using JS.

    const connection = new signalR.HubConnectionBuilder()
        .withURL("/YourHub")
        .build();

Then after the connection is made, you can make the method to send a message to the client from the server using JS as well.

    connection.on("SendMessage", message => {
        document.getElementById("IdOfElementToDisplayMessage").innerHTML = message;
    });

Finally add:

    connection.start().catch(err => console.error(err.toString()));

Now you have established the connection to the hub via client side and can now reference the connection to the hub from the IHubContext. To send a message from the server to the client you can use _hubContext.

In your case you can call Notify() and then await _hubContext.Clients.All.SendAsync("SendMessage", "something changed, Yo"); which should send your message to the SendMessage method created in JS: connection.on("SendMessage", message => { ...

If your _hubContext variable is null during the execution then the injection of the IHubContext needs to be checked.

Upvotes: 0

Related Questions