How to trigger a SignalR command outside of the front end

1.2k Views Asked by At

I feel like I'm either missing the point of the SignalR service or I've got the architecture of it wrong.

Using an Azure SignalR service I've got the front-end to front-end communication working as such;

Startup.cs

public void Configuration(IAppBuilder app)
{
    ConfigureAuth(app);
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    app.MapAzureSignalR(this.GetType().FullName);
}

Hub.cs

private void BroadcastMessage(string message)
{
    Clients.All.broadcastMessage(message);
}

Index.html

hubName.client.broadcastMessage = function (message) {
    alert(message);
};

$('#btn').click(function () {
    hubName.server.broadcastMessage("Hello World");
});

This is fine, and when I need the back-end to trigger a message I am using the following;

Functions.cs

HubConnection signalr = new HubConnection("http://localhost:57690/");
IHubProxy hub = null;
if (signalr.State == ConnectionState.Disconnected)
{
    hub = signalr.CreateHubProxy(hubName);
    signalr.Start().Wait();
}
hub.Invoke(functionName, args);
signalr.Stop();

While this works it leaves me wondering if I have implemented it wrong as this leaves http://localhost:57690/signalr/hubs open to posts from any source.

In the front-end, I have to provide the Endpoint=https://appName.service.signalr.net;AccessKey=xxxxx setting but not from the back-end.

As well as a security concern, this leaves me questioning what purpose the SignalR service has in this.

Surely there must be a better way to invoke signalr from the back-end.

Many Thanks,

Tom

1

There are 1 best solutions below

2
ilkerkaran On

In the Startup.cs you gotta have a line of code like this

services.AddSignalR().AddAzureSignalR();

By not passing a parameter to AddAzureSignalR(), this code uses the default configuration key, Azure:SignalR:ConnectionString, for the SignalR Service resource connection string.

You can find more info in this article