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);
}
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,
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 -
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.
Create a client application (Message receiver, most of times) by HubConnectionBuilder library using any client-side languages varying from JS, TS, .Net Console,..
Create a Message broadcaster application by Azure SignalR Connection String to send signals via Azure SignalR Service.
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.
Client can post signals only via Web API Central Hub that too when Web API implemented that event.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With