1
votes

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. I created a c# console client and connected to Asp.Net Web Application's SignalR Hub using Microsoft.AspNetCore.SignalR.Client library.

Questions:

  1. Should I always implement a middleware like Asp.Net Web Application and rely on it to create and talk over Hub? Yes, based on Samara & other sources, we have a couple of choices.

    • Consume REST API from console app to host a hub (Azure SignalR) as a server and serve the clients\ subscribers as portal. This supports uni-directional messaging.
    • Consume REST API from Azure Functions (make it serverless) to host a hub (Azure SignalR) as a server and serve the clients\ subscribers as portal. This supports uni-directional messaging.
    • Host a hub (Azure SignalR) from Asp.Net managed Web API with auth() layer and cater clients via API. This supports fully blown bi-directional messaging.
  2. Is it not possible to directly talk to Azure SignalR Service from c# console to create a Hub and send my messages over Hub?

    • Consume REST API from console app to host a hub (Azure SignalR) as a server and serve the clients\ subscribers as portal. This supports uni-directional messaging.
    • Consume REST API from Azure Functions (make it serverless) to host a hub (Azure SignalR) as a server and serve the clients\ subscribers as portal. This supports uni-directional messaging.

I am new to Azure SignalR Service; please assist me on above questions. My questions are almost answered. Hope it helps!

P.S. People does not have real answers, stay away

1
You can use whatever type of project you want to in order to achieve your goal. SignalR is just a system for implementing websocket connections, which is usually done in conjuction with web services, but that isn't a requirement.SamaraSoucy
So, from console client, I found a github project sample called 'Serverless'. It talks to Azure SignalR Service to create a hub, broadcast messages to individual user to all users. But individual user could not post messages to Hub. How to make it two data sync like chat applications.Ashokan Sivapragasam
@SamaraSoucy-MSFT ,Azure SignalR service integration with Asp.Net Apps and Console client seems far different to each other. Integration with SignalR is not seamless.Ashokan Sivapragasam
@SamaraSoucy-MSFT, Thanks in advance for your assistance!Ashokan Sivapragasam

1 Answers

1
votes

I think I understand your question a bit better. I think you are talking about the sample code here? https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Serverless

If you want to make a chat application you would modify the code to combine the two functions. I was able to do this by updating the program.cs to run both code sets. It isn't a complete chat client- all messages still say they are coming from the other user, but I hope it gives you a better idea of how this works.

    app.Command("client", cmd =>
    {
        cmd.Description = "Start a client to listen to the service";
        cmd.HelpOption("--help");

        var userId = cmd.Argument("<userId>", "Set User ID");

        cmd.OnExecute(async () =>
        {
            var connectionString = connectionStringOption.Value() ?? configuration["Azure:SignalR:ConnectionString"];

            if (string.IsNullOrEmpty(connectionString) || !hubOption.HasValue())
            {
                MissOptions();
                return 0;
            }

            var client = new ClientHandler(connectionString, hubOption.Value(), userId.Value);

            await client.StartAsync();

//Add the server to the client so we can talk both ways
            var server = new ServerHandler(connectionString, hubOption.Value());
            await server.Start();


            Console.ReadLine();
            await client.DisposeAsync();

            return 0;
        });
    });

There is something to keep in mind, and why most of these examples add an API layer over the top of the SignalR service. In order to create a connection to the service, you are using a key. If you embed that into your console app and then distibute it, anyone with the app now has the ability to control all of your hubs by extracting the key. Unless you trust all your users with this power, then adding an API layer is the way to go- the user authenticates to the API, and the API decides what permissions they have on the hub.