May 29, 2023 by Anuraj Estimated read time : 7 mins
This post is about implementing database change notification using SignalR and Azure Functions. In this post we will using Azure Function with SQL trigger for change identification and Azure SignalR binding for notifications. Here is the basic architecture of the implementation.
We will be using a simple note taking application - in the application if some one adding a note, it will be automatically populated to all the users who are online. Here is the database schema.
To support SQL trigger, we need to enable change tracking in SQL Server. We can do this by executing the following code.
Next, we need to create an Azure Function with SQL trigger. To do this, we need to create an Azure Function with Http Trigger and add the Microsoft.Azure.WebJobs.Extensions.Sql nuget package. Next we need to modify the code like this.
And here is the Note class.
And finally we need to configure SignalR binding to the Azure Function. To do this, first we need to add reference of Microsoft.Azure.WebJobs.Extensions.SignalRService nuget package. And modify the Azure Function like this.
To configure SignalR in the Azure Function, we need to configure one more Azure Function - Negotiate - which will be called by SignalR client. Here is the implementation.
Now we have completed the implementation. Next we can configure a client application which will show the updates when a user insert a note to the table. For making the implementation simple, I am creating a new function which returns a HTML file.
And in the index.html page we need to add the following code.
The index.html file created in a content folder. And to copy the file to the output directory we can add the following code in the Project file.
This way we can implement Database changes notifications using Azure SignalR service and Azure Functions.