2

I have a .Net Core Web MVC Application and I want to send a notification to a client with Azure SignalR, when in CosmosDB the change feed is triggered.

FeedToSignalR trigger on new data in CosmosDB and broadcast it through SignalR to a clients.

SignalRConfiguration initialize the SignalR Websocket connection.

The problem is I don’t know how I can call this methods.

Can I call the methods in my Program.cs or in the Startup.cs?

public static class SignalRConfiguration
    {
        private static AzureSignalR signalR = new AzureSignalR(Environment.GetEnvironmentVariable("AzureSignalRConnectionString"));

        /// <summary>
        /// This HttpTriggered function returns the SignalR configuration to the web client.
        /// </summary>
        [FunctionName("SignalRConfiguration")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequestMessage req, TraceWriter log)
        {
            return req.CreateResponse(HttpStatusCode.OK, 
                new {
                    hubUrl = signalR.GetClientHubUrl("cosmicServerlessHub"),
                    accessToken = signalR.GenerateAccessToken("cosmicServerlessHub")
                });
        }
}

public static class FeedToSignalR
    {
        private static AzureSignalR signalR = new AzureSignalR(Environment.GetEnvironmentVariable("AzureSignalRConnectionString"));

        /// <summary>
        ///  This function Triggers upon new documents in the Cosmos DB database and broadcasts them to SignalR connected clients.
        /// </summary>
        [FunctionName("FeedToSignalR")]
        public static async Task Run([CosmosDBTrigger(
            databaseName: "ToDoList",
            collectionName: "Items",
            ConnectionStringSetting = "AzureCosmosDBConnectionString",
            LeaseConnectionStringSetting = "AzureCosmosDBConnectionString",
            CreateLeaseCollectionIfNotExists = true,
            LeaseCollectionName = "leases")]IReadOnlyList<Document> documents, TraceWriter log)
        {
            if (documents != null && documents.Count > 0)
            {
                var broadcast = documents.Select((d) => new
                {
                    id = d.GetPropertyValue<string>("id"),
                    price = d.GetPropertyValue<string>("price")
                });

                await signalR.SendAsync("cosmicServerlessHub", "NewMessages", JsonConvert.SerializeObject(broadcast));
            }
        }
}
dev4678
  • 121
  • 4

2 Answers2

1

These are two Functions that each get triggered by different events. Your FeedToSignalR is using the Cosmos DB Trigger which will trigger itself automatically when a new document is added in the Items collection as described in the comment above the function's code.

SignalRConfiguration on the other hand is using an HTTP Trigger which gets called whenever there is an HTTP call to it's HTTP endpoint.

If I recognize the code correctly, it is coming from https://github.com/ealsur/serverlessnotifications

Based on the explanation:

  1. The browser will do an HTTP call to the SignalRConfiguration function to get the configuration
  2. When a new message is typed, it will save that message in Cosmos DB by invoking another Function.
  3. That new document in the collection, will then trigger the Cosmos DB Trigger Function who will in turn send it through Signal R.
Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • The only thing that I need to do is, to put the two json files in my project to bind the HTTP Trigger and the Cosmos DB Trigger? How the .Net Core project know to call that json files? – dev4678 Feb 04 '19 at 00:36
  • I don't understand. Is your question on how the Trigger works or how to make that sample work? If it's the former, I covered it on my answer. If it's the later, just take the repository as is (clone), the instructions are on the [README](https://github.com/ealsur/serverlessnotifications/blob/master/README.md#how-can-i-use-this-repo). – Matias Quaranta Feb 04 '19 at 16:34
0

Yes, this is exactly the code. I think the problem is that I don't have the json “function.json” like here "https://learn.microsoft.com/de-de/azure/azure-functions/functions-bindings-cosmosdb-v2#trigger---c-script-example" to bind the Cosmos DB Trigger and the other json to bind HTTP Trigger.

This “https://anthonychu.ca/post/cosmosdb-real-time-azure-functions-signalr-service/” example does exactly what I want, but Mr. Chu used the .Net Standard, I need do this in .Net Core MVC or better .Net Framework MVC. On the webpage is a picture of the architecture, that describes what I want to do, I tried do reproduced the Step 3 to 5 with your example.

The only thing that I need to do is, to put the two json files in my project to bind the HTTP Trigger and the Cosmos DB Trigger? How the .Net Core project know to call that json files?

dev4678
  • 121
  • 4