Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not connect to Azure SignalR Hub

Tags:

c#

signalr

azure

I am trying to post real time messages to Azure SignalR Hub. But it I could not connect to my Azure SignalR Hub itself. I tried various solutions. It did not work as expected. Could you assist me here? No documentation for this version of SignalR Client.

public static async Task BroadcastMessageAsync()
{
    var signalrUrl = "https://myazuresignalr.service.signalr.net";
    var hubConnection = new HubConnection(signalrUrl);
    hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
    hubConnection.Closed += async () =>
    {
        await hubConnection.Start();
    };

    var hubProxy = hubConnection.CreateHubProxy("chat");

    hubProxy.On<string, string>("ReceiveMessage", (user, message) =>
    {
        Console.WriteLine(user + " | " + message);
    });

    try
    {
        await hubConnection.Start();
        Console.WriteLine("Connection started");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    await hubProxy.Invoke("SendMessage", "user", "message").ConfigureAwait(false);
}
like image 359
Ashokan Sivapragasam Avatar asked Oct 23 '25 10:10

Ashokan Sivapragasam


2 Answers

Looks like, after all, HubConnection from Microsoft.AspNetCore.SignalR.Client is intended for connecting to SignalR Hub managed at the Middleware like Asp.Net Web App\ Api. It was not intended to connect to direct Azure SignalR service endpoint.

Wrong:

var signalrUrl = "https://myazuresignalr.service.signalr.net";

Correct: For chat client

var signalrUrl = "https://myazuresignalr.service.signalr.net/client/?hub=yourHubName";

Correct: For chat server

var signalrUrl = "https://myazuresignalr.service.signalr.net/api/v1/hubs/yourHubName

Now, I am able to connect to Azure SignalR Service from Asp.Net Web Application using its Access-Key. Asp.Net Web App creates and hosts a hub over Azure SignalR Service. From c# console application, I am connecting to Asp.Net Application to talk to Azure SignalR Service.

There is no clear documentation at the surface for Azure SignalR Service to declare that Client library is consuming REST API version of SignalR.

Hope it can assist and help someone else!

Summing up,

  1. Should I always implement a middleware like Asp.Net Web Application and rely on it to create and talk over Hub? Is it not possible to directly talk to Azure SignalR Service from c# console to create a Hub and send my messages over Hub?

    [OP]: No, not always. There is another solution apart from what I wrote above. This tutorial helps me talk to Azure SignalR Service directly via REST API.

- UPDATE -

  1. Create a central hub for Azure Signal Service using any server-side language (I chose, Asp.Net Web API). This is a best practice to have a middle-man like Web API to authorize the client requests before posting to Azure SignalR Service. Also, to mask the Azure SignalR Connection String from the clients.

  2. Create a client application (Message receiver, most of times) by HubConnectionBuilder library using any client-side languages varying from JS, TS, .Net Console,..

  3. Create a Message broadcaster application by Azure SignalR Connection String to send signals via Azure SignalR Service.

  4. Server can post any new signal events over Hub while client can listen to those dynamic events, provided client is subscribed to those dynamic events.

  5. Client can post signals only via Web API Central Hub that too when Web API implemented that event.

like image 134
Ashokan Sivapragasam Avatar answered Oct 26 '25 00:10

Ashokan Sivapragasam


https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.client.hubconnection?view=aspnetcore-2.2

Remarks A HubConnection should be created using HubConnectionBuilder. Before hub methods can be invoked the connection must be started using StartAsync(CancellationToken). Clean up a connection using StopAsync(CancellationToken) or DisposeAsync().

sample

HubConnection _connection = new HubConnectionBuilder()
                .WithUrl(url, option =>
                {
                    option.AccessTokenProvider = () =>
                    {
                        return Task.FromResult(//// Access Token ///);
                    };
                }).Build();

Take a look at these samples

https://github.com/aspnet/AzureSignalR-samples/tree/master/samples

like image 21
Rahul Ruikar Avatar answered Oct 25 '25 23:10

Rahul Ruikar