1

I have 2 ASP.NET Core Razor web apps. Each app will be using SignalR to communicate with web app clients and mobile clients. Because of my intended use, I have setup both web apps as Hub and Client, where the Client is using the .NET SignalR Client. Each of the 2 web apps have the following:

        static internal HubConnection Connection; // In the Startup class

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Connection = new HubConnectionBuilder()
                 // Where the URL is for the OTHER web app
                .WithUrl("https://localhost:44386/NotificationHub")
                .Build();
        }

Each project also has a NotificationHub class, derived from Hub, in the Hubs folder.

In the Startup ConfigureServices method in each app I have as the last statement:

services.AddSignalR();

In the Startup Configure method in each app I have the following immediately before the call to UseMvc:

app.UseSignalR(routes =>
{
    routes.MapHub<NotificationHub>("/NotificationHub");
});

In each of the NotificationHub classes I have:

    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }

So, I am unsure how to initially connect from one Client to a Hub and I am also unsure if I am using the URL correctly.

JNickVA1
  • 418
  • 7
  • 23

1 Answers1

2

For using js Client :

1) Install signalr npm : npm install @aspnet/signalr

2) Add the reference in required page

3) Add Connection Object code

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/NotificationHub")
    .configureLogging(signalR.LogLevel.Information)
    .build();
connection.start().catch(err => console.error(err.toString()));

4) Call your require method

connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));

For .Net Client

1) install nuget: Install-Package Microsoft.AspNetCore.SignalR.Client

2)

 HubConnection connection  = new HubConnectionBuilder()
            .WithUrl("https://localhost:44386/NotificationHub")
            .Build();     

 await connection.StartAsync();

 await connection.InvokeAsync("SendMessage", 
                    "user", "message");

you can start the connection with button click or on sending message

You can find more details in below link :

https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-2.1

Hany Habib
  • 1,377
  • 1
  • 10
  • 19
  • I should have mentioned, I want to use the .NET client in both apps – JNickVA1 Jun 29 '18 at 12:00
  • 1
    OK, but where do I put the call: await connection.StartAsync(); ? – JNickVA1 Jun 29 '18 at 12:16
  • you can add on event handler or just in ctor if you want always to be connected – Hany Habib Jun 29 '18 at 13:21
  • I managed to get the Hub and the Client working in one direction so far. I haven't yet tested running a Hub AND a Client in each web app. An obvious question arises thou - How do I determine if the StartAsync actually connected? – JNickVA1 Jun 29 '18 at 16:19
  • Glad to help here you go https://stackoverflow.com/questions/47056690/how-to-check-success-of-hubconnection-startasync-in-signalr-for-asp-net-core-2-0 ... dont forget to mark if saved your day :))) – Hany Habib Jun 29 '18 at 20:31