0

I’m developing a app that used CosmosDB to store data and then when anyone updates the data i want the clients to be updated. For this i have decided to use the changefeed and then Azure Functions and Azure SignalR.

I have set up 2 functions. A negotiate function (This one works and the clients connect correctly to the SignalR server) And a OnDocumentsChanged function, and my problem is getting the function to actually sending the message, when something is changed.

I have the following function:

[FunctionName("OnDocumentsChanged")]
    public static async Task Run(
        [CosmosDBTrigger(
        databaseName: "NewOrder",
        collectionName: "NewOrder",
        CreateLeaseCollectionIfNotExists = true,
        ConnectionStringSetting = "myserver_DOCUMENTDB",
        LeaseCollectionName = "leases")]
            IReadOnlyList<Document> updatedNewOrder,
        [SignalR(ConnectionStringSetting = "AzureSignalRConnectionString",  HubName = "NewOrder")] IAsyncCollector<SignalRMessage> signalRMessages,
        ILogger log)
    {
        if (updatedNewOrder != null && updatedNewOrder.Count > 0)
        {
            foreach (var Orders in updatedNewOrder)
            {
                await signalRMessages.AddAsync(new SignalRMessage
                {
                    Target = "NewOrderUpdated",
                    Arguments = new[] { Orders.Id }
                });
            }
            
        }

    }

I can see that it is correctly triggered when a change is made to the database, but no messages are send.

I guess I’m missing a out part that actually send the SignalRMessages I’m just not sure how to implement.

Thanks.

Kashyap
  • 15,354
  • 13
  • 64
  • 103
Kasper S Mathiesen
  • 669
  • 1
  • 7
  • 17
  • 1. Not a C# expert, but isn't `await xxx.AddAsync` an oxymoron? 2. Can you log the return value of `signalRMessages.AddAsync` and `await`? 3. Are you running locally or in cloud? If in cloud then go to `Portal->FuncApp->Functions->your-function->Monitor` and click on one of the runs. I've seen exception stacks in that log that were missing in App Insights. – Kashyap Jan 26 '21 at 21:34
  • Await and then the mothod is called *Async is normal C# naming. Where all Async methods should be named Async. Im running th function in cloud. I tried to remove the foreach and just add a [0] and then the function works. – Kasper S Mathiesen Jan 27 '21 at 09:28
  • What I meant is the purpose of an Async operation is that you want to perform a lot of them without waiting for each one. So you should `AddAsync(1), AddAsync(2), AddAsync(3)` then `await(1, 2, 3)` instead of `AddAsync(1), await(1, 2, 3)`, `AddAsync(2), await(1, 2, 3)`, `AddAsync(3), await(1, 2, 3)` – Kashyap Jan 28 '21 at 18:25

0 Answers0