Documente Academic
Documente Profesional
Documente Cultură
Azure Functions
Introduction
Technologies used
Azure Storage - Host the static website for the chat client UI
Azure Functions - Backend API for creating and retrieving chat messages
Azure Cosmos DB - Store chat messages
Azure SignalR Service - Broadcast new messages to connected chat clients
Prerequisites
The following software is required to build this tutorial.
Git
Node.js (Version 10.x)
.NET SDK (Version 2.x, required for Functions extensions)
Azure Functions Core Tools (Version 2)
Visual Studio Code (VS Code) with the following extensions
o Azure Functions
o Live Server
Postman (optional, for testing Azure Functions)
===
Name Value
API SQL
4.
5. Click Create.
Name Value
Resource group Select the same resource group as the Cosmos DB account
4.
5. Click Create.
===
i. Open the Command Palette in VS Code by selecting View > Command Palette from the
menu (shortcut Ctrl-Shift-P, macOS: Cmd-Shift-P).
ii. Search for the "Azure Functions: Create New Project" command and select it.
iii. The main project folder should appear. Select it (or use "Browse" to locate it).
iv. In the prompt to choose a language, select JavaScript.
Install function app extensions
This tutorial uses Azure Functions bindings to interact with Azure Cosmos DB and Azure SignalR
Service. These bindings are available as extensions that need to be installed using the Azure
Functions Core Tools CLI before they can be used.
1. Open a terminal in VS Code by selecting View > Integrated Terminal from the menu (Ctrl-`).
3. {
4. "IsEncrypted": false,
5. "Values": {
6. "AzureSignalRConnectionString": "<signalr-connection-string>",
7. "CosmosDBConnectionString": "<cosmosdb-connection-string>",
8. "WEBSITE_NODE_DEFAULT_VERSION": "10.6.0",
9. "FUNCTIONS_WORKER_RUNTIME": "node"
10. },
11. "Host": {
12. "LocalHttpPort": 7071,
13. "CORS": "*"
14. }
15. }
o Enter the Azure SignalR Service connection string into a setting
named AzureSignalRConnectionString. Obtain the value from the Keys page in the
Azure SignalR Service resource in the Azure portal; either than primary or secondary
connection string can be used.
o Enter the Azure Cosmos DB connection string into a setting
named CosmosDBConnectionString. Obtain the value from the Keys page in the
Cosmos DB account in the Azure portal; either than primary or secondary connection
string can be used.
o The WEBSITE_NODE_DEFAULT_VERSION setting is not used locally, but is required when
deployed to Azure.
o The Host section configures the port and CORS settings for the local Functions host.
===
CreateMessage function
2. Search for and select the Azure Functions: Create Function command.
Name Value
Name CreateMessage
6. {
7. "disabled": false,
8. "bindings": [
9. {
10. "authLevel": "anonymous",
11. "type": "httpTrigger",
12. "direction": "in",
13. "name": "req",
14. "route": "messages",
15. "methods": [
16. "post"
17. ]
18. },
19. {
20. "type": "http",
21. "direction": "out",
22. "name": "res"
23. },
24. {
25. "name": "cosmosDBMessage",
26. "type": "cosmosDB",
27. "databaseName": "chat",
28. "collectionName": "messages",
29. "createIfNotExists": true,
30. "connectionStringSetting": "CosmosDBConnectionString",
31. "direction": "out"
32. }
33. ]
34. }
o Changes the route to messages and restricts the HTTP trigger to the POST HTTP
method.
o Adds a Cosmos DB output binding that saves documents to a collection
named messages in a database named chat.
36. Open CreateMessage/index.js to view the body of the function. Modify the content of the
file to the following.
1. To run the function app locally, press F5 in VS Code. If this is the first time, the Azure
Functions host will start in the VS Code integrated terminal.
2. When the functions runtime is successfully started, the terminal output will display a URL for
the local CreateMessage endpoint (by default, it is http://localhost:7071/api/messages).
6. https://raw.githubusercontent.com/Azure-Samples/functions-serverless-chat-app-
tutorial/master/requests/SignalRChat.postman_collection.json
This loads a collection of HTTP requests for testing the function app locally. Click on
the Collections tab in Postman to see it.
8. Confirm the URL matches the one outputted by the function host and there is JSON
message in the request body.
9. Click Send. The function app should return an HTTP status of 200.
10. In the Azure portal, open the Cosmos DB account resource you created earlier.
11. Using the Cosmos DB Data Explorer, locate the messages collection in the chat database.
The message sent from Postman should appear as a document in the collection.
12. Click the Disconnect button to disconnect the debugger from the function host.
GetMessages function
2. Search for and select the Azure Functions: Create Function command.
Name Value
Name GetMessages
6. {
7. "disabled": false,
8. "bindings": [
9. {
10. "authLevel": "anonymous",
11. "type": "httpTrigger",
12. "direction": "in",
13. "name": "req",
14. "route": "messages",
15. "methods": [
16. "get"
17. ]
18. },
19. {
20. "type": "http",
21. "direction": "out",
22. "name": "res"
23. },
24. {
25. "name": "messages",
26. "type": "cosmosDB",
27. "databaseName": "chat",
28. "collectionName": "messages",
29. "sqlQuery": "select * from c order by c._ts desc",
30. "connectionStringSetting": "CosmosDBConnectionString",
31. "direction": "in"
32. }
33. ]
34. }
o Changes the route to messages and restricts the HTTP trigger to the GET HTTP
method.
o Adds a Cosmos DB input binding that retrieves documents (in reverse chronological
order) from a collection named messages in a database named chat.
36. Open GetMessages/index.js to view the body of the function. Modify the content of the
file to the following.
This function takes the documents retrieved from Cosmos DB (chat messages in reverse
chronological order) and returns them in the HTTP response body.
1. To run the function app locally, press F5 in VS Code. If this is the first time, the Azure
Functions host will start in the VS Code integrated terminal.
2. When the functions runtime is successfully started, the terminal output will display URLs for
the local CreateMessage and GetMessages endpoints (by default, they
are http://localhost:7071/api/messages).
3. (See above to open Postman and import a collection) In Postman, in the SignalR
Chat collection, select the Get messages request.
4. Confirm the URL matches the one outputted by the function host and the HTTP method
is GET.
5. Click Send. The function app should return the messages from Cosmos DB.
6. Press the Disconnect button to disconnect the debugger from the function host.
===
1. In VS Code, create a new folder named content at the root of the main project folder.
5. Press F5 to run the function app locally (it may already be running) and attach a debugger.
6. With index.html open, start Live Server by opening the VS Code command palette (Ctrl-
Shift-P, macOS: Cmd-Shift-P) and selecting Live Server: Open with Live Server. Live Server
will open the application in a browser.
7. When the application prompts for a username, enter one. If you tested the function earlier,
messages from your testing session will appear.
8. Enter a message in the chat box and press enter. Refresh the application to see new
messages.
Next, you will integrate Azure SignalR Service into your application to allow messages to appear in
real-time.
===
SignalRInfo function
2. Search for and select the Azure Functions: Create Function command.
Name Value
Name SignalRInfo
28. Open SignalRInfo/index.js to view the body of the function. Modify the content of the file
to the following.
This function takes the SignalR connection information from the input binding and returns it
to the client in the HTTP response body.
1. To run the function app locally, press F5 in VS Code. If this is the first time, the Azure
Functions host will start in the VS Code integrated terminal.
2. When the functions runtime is successfully started, the terminal output will display URLs for
the local endpoints, including SignalRInfo (by default, they
are http://localhost:7071/api/SignalRInfo).
3. (See above to open Postman and import a collection) In Postman, in the SignalR
Chat collection, select the Get SignalR info request.
4. Confirm the URL matches the one outputted by the function host and the HTTP method
is GET.
5. Click Send. The function app should return connection information for SignalR Service.
6. Press the Disconnect button to disconnect the debugger from the function host.
37. Open CreateMessage/index.js to view the body of the function. Modify the content of the
file to the following. This adds a line to output new chat messages to all clients connected
to the SignalR Service hub.
1. Press F5 to run the function app locally (it may already be running) and attach a debugger.
2. With index.html open, start Live Server by opening the VS Code command palette (Ctrl-
Shift-P, macOS: Cmd-Shift-P) and selecting Live Server: Open with Live Server. Live Server
will open the application in a browser.
3. Now, new messages will appear as soon as they are sent. Open the app in more than one
browser to see the real-time capabilities in action.
===
Deploy to Azure
You have been running the function app and chat application locally. You will now deploy them to
Azure.
2. Search for and select the Azure Functions: Deploy to Function App command.
Name Value
Select the same resource group as the Cosmos DB account and SignalR
Resource group
Service instance
Storage account
Enter a unique name (3-24 characters, alphanumeric only)
name
2. Search for and select the Azure Functions: Upload local settings command.
Name Value
Local settings are uploaded to the function app in Azure. If prompted to overwrite existing
settings, select Yes to all.
Enable function app cross origin resource sharing (CORS)
Although there is a CORS setting in local.settings.json, it is not propagated to the function app in
Azure. You need to set it separately.
2. Search for and select the Azure Functions: Open in portal command.
3. Select the subscription and function app name to open the function app in the Azure portal.
3. In VS Code, open index.html and replace the value of window.apiBaseUrl with the function
app's URL.
1. Click on the New (+) button for creating a new Azure resource.
Name Value
Resource group Select the same resource group as the Cosmos DB account
Storage account name A unique name for the blob storage account
Performance Standard
5. Click Create.
8. Select Enable.
11. Click on the $web link on the page to open the blob container.
12. Click Upload and upload all the files in the content folder.
13. Go back to the Static website page. Copy the Primary endpoint address and open it in a
browser.
The chat application will appear. Congratulations on creating and deploying a serverless chat
application to Azure!