The following project contains a sample project showcasing the use of the Cosmos DB Trigger to work with data as it's inserted/modified in a Cosmos DB container.
This reusable solution creates a complete serverless scenario for a social application that stores data in a Cosmos DB container, uses Functions for hosting and event processing, Azure Cognitive Services for content analysis, and Azure SignalR for websocket client messaging.
The solution contains a group of Functions:
- Azure Functions as a serverless Web host for static html/js/css files (the social UI).
- Azure Functions HTTP API to receive client calls to store data in Azure Cosmos DB using the Output Binding.
- Azure Functions CosmosDB Trigger that takes new social posts, send them to Cognitive Services using a static client, and distributing the result to all Signal R connected clients.
- The web client is using Azure SignalR's npm package for connectivity and transport protocol resolution.
- Another Azure Functions HTTP API that will send the required information to the Web client to initialize SignalR Websocket connection.
In order to support custom routes (particularly for the static Web host), it implements Azure Functions Proxies through a proxies.json file. So when browsing the base URL, it is instead calling one of the HTTP triggered Functions.
-
When the Web client loads the static resources, it pulls the SignalR configuration from SignalRConfiguration.
-
It will then negotiate with Azure SignalR the best transport protocol.
-
When the user writes a post, it will save it to Azure Cosmos DB via an Ajax call to SavePost
-
The CosmosDBAnalyticsTrigger will trigger and broadcast it to all Azure SignalR connected clients.
You can use Visual Studio Code, Visual Studio or even the Azure Functions' CLI. You only need to customize the local.settings.json file to match the Cosmos DB Connection String, Database name, and Collection/Container name on which you want the posts to be saved. You also need an Azure Signal R Connection String and the Key to a Cognitive Services Text Analytics API.
Additional step: Change the Region of your Cognitive Services deployment here.
Once the solution is running, open your browser in the base address informed by the Azure Function's runtime (something along the lines of http://localhost:<some-port>
).