Skip to content

Commit 964e435

Browse files
authored
Merge pull request #199826 from JialinXin/patch-9
[WebPubSub] Add tutorial for SWA integration
2 parents b99bcd9 + 812edde commit 964e435

File tree

3 files changed

+213
-3
lines changed

3 files changed

+213
-3
lines changed
62 KB
Loading

articles/azure-web-pubsub/toc.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
- name: Serverless development
3434
expanded: true
3535
items:
36-
- name: Build a notification app with Azure Functions
36+
- name: Build a notification app by using Azure Functions
3737
href: tutorial-serverless-notification.md
3838
- name: Build a real-time chat app with client authentication
3939
href: quickstart-serverless.md
40-
- name: Visualize IoT data with Azure Function
41-
href: tutorial-serverless-iot.md
40+
- name: Build a real-time chat app by using an Azure Static Web App
41+
href: tutorial-serverless-static-web-app.md
42+
- name: Visualize IoT data by using an Azure Function
43+
href: tutorial-serverless-iot.md
4244
- name: Publish and subscribe messages
4345
href: tutorial-pub-sub-messages.md
4446
- name: Build a chat app
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
title: Tutorial - Create a serverless chat app using Azure Web PubSub service and Azure Static Web Apps
3+
description: A tutorial for how to use Azure Web PubSub service and Azure Static Web Apps to build a serverless chat application.
4+
author: JialinXin
5+
ms.author: jixin
6+
ms.service: azure-web-pubsub
7+
ms.topic: tutorial
8+
ms.date: 06/01/2022
9+
---
10+
11+
# Tutorial: Create a serverless chat app using Azure Web PubSub service and Azure Static Web Apps
12+
13+
The Azure Web PubSub service helps you build real-time messaging web applications using WebSockets. And with Azure Static Web Apps, you can automatically build and deploy full stack web apps to Azure from a code repository conveniently. In this tutorial, you learn how to use Azure Web PubSub service and Azure Static Web Apps to build a serverless real-time messaging application under chat room scenario.
14+
15+
In this tutorial, you learn how to:
16+
17+
> [!div class="checklist"]
18+
> * Build a serverless chat app
19+
> * Work with Web PubSub function input and output bindings
20+
> * Work with Static Web Apps
21+
22+
## Overview
23+
24+
:::image type="content" source="media/tutorial-serverless-static-web-app/tutorial-serverless-static-web-app.png" alt-text="Diagram showing Azure Web PubSub work with Static Web App." border="false":::
25+
26+
* GitHub along with DevOps provide source control and continuous delivery. So whenever there's code change to the source repo, Azure DevOps pipeline will soon apply it to Azure Static Web App and present to endpoint user.
27+
* When a new user is login, Functions `login` API will be triggered and generate Azure Web PubSub service client connection url.
28+
* When client init the connection request to Azure Web PubSub service, service will send a system `connect` event and Functions `connect` API will be triggered to auth the user.
29+
* When client send message to Azure Web PubSub service, service will send a user `message` event and Functions `message` API will be triggered and broadcast the message to all the connected clients.
30+
* Functions `validate` API will be triggered periodically for [CloudEvents Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#4-abuse-protection) purpose, when the events in Azure Web PubSub are configured with predefined parameter `{event}`, that is, https://$STATIC_WEB_APP/api/{event}.
31+
32+
> [!NOTE]
33+
> Functions APIs `connect` and `message` will be triggered when Azure Web PubSub service is configured with these 2 events.
34+
35+
## Prerequisites
36+
37+
* [GitHub](https://github.com/) account
38+
* [Azure](https://portal.azure.com/) account
39+
* [Azure CLI](/cli/azure) (version 2.29.0 or higher) or [Azure Cloud Shell](../cloud-shell/quickstart.md) to manage Azure resources
40+
41+
## Create a Web PubSub resource
42+
43+
1. Sign in to the Azure CLI by using the following command.
44+
45+
```azurecli-interactive
46+
az login
47+
```
48+
49+
1. Create a resource group.
50+
51+
```azurecli-interactive
52+
az group create \
53+
--name my-awps-swa-group \
54+
--location "eastus2"
55+
```
56+
57+
1. Create a Web PubSub resource.
58+
59+
```azurecli-interactive
60+
az webpubsub create \
61+
--name my-awps-swa \
62+
--resource-group my-awps-swa-group \
63+
--location "eastus2" \
64+
--sku Free_F1
65+
```
66+
67+
1. Get and hold the access key for later use.
68+
69+
```azurecli-interactive
70+
az webpubsub key show \
71+
--name my-awps-swa \
72+
--resource-group my-awps-swa-group
73+
```
74+
75+
```azurecli-interactive
76+
AWPS_ACCESS_KEY=<YOUR_AWPS_ACCESS_KEY>
77+
```
78+
Replace the placeholder `<YOUR_AWPS_ACCESS_KEY>` from previous result `primaryConnectionString`.
79+
80+
## Create a repository
81+
82+
This article uses a GitHub template repository to make it easy for you to get started. The template features a starter app used to deploy using Azure Static Web Apps.
83+
84+
1. Navigate to the following template to create a new repository under your repo:
85+
1. [https://github.com/Azure/awps-swa-sample/generate](https://github.com/login?return_to=/Azure/awps-swa-sample/generate)
86+
1. Name your repository **my-awps-swa-app**
87+
88+
Select **`Create repository from template`**.
89+
90+
## Create a static web app
91+
92+
Now that the repository is created, you can create a static web app from the Azure CLI.
93+
94+
1. Create a variable to hold your GitHub user name.
95+
96+
```azurecli-interactive
97+
GITHUB_USER_NAME=<YOUR_GITHUB_USER_NAME>
98+
```
99+
100+
Replace the placeholder `<YOUR_GITHUB_USER_NAME>` with your GitHub user name.
101+
102+
1. Create a new static web app from your repository. As you execute this command, the CLI starts GitHub interactive login experience. Following the message to complete authorization.
103+
104+
```azurecli-interactive
105+
az staticwebapp create \
106+
--name my-awps-swa-app \
107+
--resource-group my-awps-swa-group \
108+
--source https://github.com/$GITHUB_USER_NAME/my-awps-swa-app \
109+
--location "eastus2" \
110+
--branch main \
111+
--app-location "src" \
112+
--api-location "api" \
113+
--login-with-github
114+
```
115+
> [!IMPORTANT]
116+
> The URL passed to the `--source` parameter must not include the `.git` suffix.
117+
118+
1. Navigate to **https://github.com/login/device**.
119+
120+
1. Enter the user code as displayed your console's message.
121+
122+
1. Select the **Continue** button.
123+
124+
1. Select the **Authorize AzureAppServiceCLI** button.
125+
126+
1. Configure the static web app settings.
127+
128+
```azurecli-interactive
129+
az staticwebapp appsettings set \
130+
-n my-awps-swa-app \
131+
--setting-names WebPubSubConnectionString=$AWPS_ACCESS_KEY WebPubSubHub=sample_swa
132+
```
133+
134+
## View the website
135+
136+
There are two aspects to deploying a static app. The first operation creates the underlying Azure resources that make up your app. The second is a GitHub Actions workflow that builds and publishes your application.
137+
138+
Before you can navigate to your new static site, the deployment build must first finish running.
139+
140+
1. Return to your console window and run the following command to list the URLs associated with your app.
141+
142+
```azurecli-interactive
143+
az staticwebapp show \
144+
--name my-awps-swa-app \
145+
--query "repositoryUrl"
146+
```
147+
148+
The output of this command returns the URL to your GitHub repository.
149+
150+
1. Copy the **repository URL** and paste it into the browser.
151+
152+
1. Select the **Actions** tab.
153+
154+
At this point, Azure is creating the resources to support your static web app. Wait until the icon next to the running workflow turns into a check mark with green background ✅. This operation may take a few minutes to complete.
155+
156+
Once the success icon appears, the workflow is complete and you can return back to your console window.
157+
158+
2. Run the following command to query for your website's URL.
159+
160+
```azurecli-interactive
161+
az staticwebapp show \
162+
--name my-awps-swa-app \
163+
--query "defaultHostname"
164+
```
165+
166+
Hold the url to set in the Web PubSub event handler.
167+
168+
```azurecli-interactive
169+
STATIC_WEB_APP=<YOUR_STATIC_WEB_APP>
170+
```
171+
172+
## Configure the Web PubSub event handler
173+
174+
Now you're very close to complete. The last step is to configure Web PubSub transfer client requests to your function APIs.
175+
176+
1. Run command to configure Web PubSub service events. It's mapping to some functions under the `api` folder in your repo.
177+
178+
```azurecli-interactive
179+
az webpubsub hub create \
180+
-n "my-awps-swa" \
181+
-g "my-awps-swa-group" \
182+
--hub-name "sample_swa" \
183+
--event-handler url-template=https://$STATIC_WEB_APP/api/{event} user-event-pattern="*" \
184+
--event-handler url-template=https://$STATIC_WEB_APP/api/{event} system-event="connect"
185+
```
186+
187+
Now you're ready to play with your website **<YOUR_STATIC_WEB_APP>**. Copy it to browser and click continue to start chatting with your friends.
188+
189+
## Clean up resources
190+
191+
If you're not going to continue to use this application, you can delete the resource group and the static web app by running the following command.
192+
193+
```azurecli-interactive
194+
az group delete --name my-awps-swa-group
195+
```
196+
197+
## Next steps
198+
199+
In this quickstart, you learned how to run a serverless chat application. Now, you could start to build your own application.
200+
201+
> [!div class="nextstepaction"]
202+
> [Tutorial: Client streaming using subprotocol](tutorial-subprotocol.md)
203+
204+
> [!div class="nextstepaction"]
205+
> [Azure Web PubSub bindings for Azure Functions](reference-functions-bindings.md)
206+
207+
> [!div class="nextstepaction"]
208+
> [Explore more Azure Web PubSub samples](https://github.com/Azure/azure-webpubsub/tree/main/samples)

0 commit comments

Comments
 (0)