Skip to content

Commit f710103

Browse files
authored
Merge pull request #184406 from JialinXin/awps-serverless-f2
[WebPubSub] Minor updates to serverless tutorials.
2 parents 1fc7777 + 47d50ef commit f710103

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

articles/azure-web-pubsub/howto-develop-eventhandler.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ The data sending from the service to the server is always in CloudEvents `binary
2222

2323
## Upstream and Validation
2424

25-
When configuring the webhook endpoint, the URL can use `{event}` parameter to define a URL template. The service calculates the value of the webhook URL dynamically when the client request comes in. For example, when a request `/client/hubs/chat` comes in, with a configured event handler URL pattern `http://host.com/api/{event}` for hub `chat`, when the client connects, it will first POST to this URL: `http://host.com/api/connect`. This can be useful when a PubSub WebSocket client sends custom events, that the event handler helps dispatch different events to different upstream. Note that the `{event}` parameter is not allowed in the URL domain name.
25+
When configuring the webhook endpoint, the URL can use `{event}` parameter to define a URL template. The service calculates the value of the webhook URL dynamically when the client request comes in. For example, when a request `/client/hubs/chat` comes in, with a configured event handler URL pattern `http://host.com/api/{event}` for hub `chat`, when the client connects, it will first POST to this URL: `http://host.com/api/connect`. The parameter can be useful when a PubSub WebSocket client sends custom events, that the event handler helps dispatch different events to different upstream. Note that the `{event}` parameter is not allowed in the URL domain name.
2626

27-
When setting up the event handler upstream through Azure portal or CLI, the service follows the [CloudEvents Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#4-abuse-protection) to validate the upstream webhook. Every registered upstream webhook URL will be validated by this mechanism. The `WebHook-Request-Origin` request header is set to the service domain name `xxx.webpubsub.azure.com`, and it expects the response having header `WebHook-Allowed-Origin` to contain this domain name or `*`.
27+
When setting up the event handler upstream through Azure portal or CLI, the service follows the [CloudEvents Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#4-abuse-protection) to validate the upstream webhook. Every registered upstream webhook URL will be validated by this mechanism. The `WebHook-Request-Origin` request header is set to the service domain name `xxx.webpubsub.azure.com`, and it expects the response to have a header `WebHook-Allowed-Origin` to contain this domain name or `*`.
2828

29-
When doing the validation, the `{event}` parameter is resolved to `validate`. For example, when trying to set the URL to `http://host.com/api/{event}`, the service tries to **OPTIONS** a request to `http://host.com/api/validate` and only when the response is valid the configure can be set successfully.
29+
When doing the validation, the `{event}` parameter is resolved to `validate`. For example, when trying to set the URL to `http://host.com/api/{event}`, the service will try to **OPTIONS** a request to `http://host.com/api/validate`. And only when the response is valid, the configuration can be set successfully.
3030

3131
For now, we do not support [WebHook-Request-Rate](https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#414-webhook-request-rate) and [WebHook-Request-Callback](https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#413-webhook-request-callback).
3232

3333
## Authentication between service and webhook
3434

3535
- Anonymous mode
3636
- Simple Auth with `?code=<code>` is provided through the configured Webhook URL as query parameter.
37-
- Use AAD Auth, check [here](howto-use-managed-identity.md) for details.
37+
- Use Azure Active Directory(Azure AD) authentication, check [here](howto-use-managed-identity.md) for details.
3838
- Step1: Enable Identity for the Web PubSub service
3939
- Step2: Select from existing AAD application that stands for your webhook web app
4040

4141
## Configure event handler
4242

4343
### Configure through Azure portal
4444

45-
Find your Azure Web PubSub service from **Azure portal**. Navigate to **Settings** and enter your hub-name. Then click **Add** to configure your server side webhook URL. Don't forget to click **Save** when finish.
45+
Find your Azure Web PubSub service from **Azure portal**. Navigate to **Settings**. Then select **Add** to configure your server-side webhook URL. For an existing hub configuration, select **...** on right side will navigate to the same editing page.
4646

4747
:::image type="content" source="media/quickstart-serverless/set-event-handler.png" alt-text="Screenshot of setting the event handler.":::
4848

49+
Then in the below editing page, you'd need to configure hub name, server webhook URL, and select `user` and `system` events you'd like to subscribe. Finally select **Save** when everything is done.
50+
51+
:::image type="content" source="media/quickstart-serverless/edit-event-handler.png" alt-text="Screenshot of editing the event handler.":::
52+
4953
### Configure through Azure CLI
5054

5155
Use the Azure CLI [**az webpubsub hub**](/cli/azure/webpubsub/hub) group commands to configure the event handler settings.
37.9 KB
Loading
-31.3 KB
Loading

articles/azure-web-pubsub/quickstart-serverless.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ In this tutorial, you learn how to:
142142
- Update `index.cs` and replace `Run` function with following codes.
143143
```c#
144144
[FunctionName("index")]
145-
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
145+
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req, ILogger log)
146146
{
147147
string indexFile = "index.html";
148148
if (Environment.GetEnvironmentVariable("HOME") != null)
@@ -203,14 +203,18 @@ In this tutorial, you learn how to:
203203
```c#
204204
[FunctionName("negotiate")]
205205
public static WebPubSubConnection Run(
206-
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
206+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
207207
[WebPubSubConnection(Hub = "simplechat", UserId = "{headers.x-ms-client-principal-name}")] WebPubSubConnection connection,
208208
ILogger log)
209209
{
210210
log.LogInformation("Connecting...");
211211
return connection;
212212
}
213213
```
214+
- Add below `using` statements in header to resolve required dependencies.
215+
```c#
216+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
217+
```
214218
215219
5. Create a `message` function to broadcast client messages through service.
216220
```bash
@@ -264,22 +268,26 @@ In this tutorial, you learn how to:
264268
```c#
265269
[FunctionName("message")]
266270
public static async Task<UserEventResponse> Run(
267-
[WebPubSubTrigger(WebPubSubEventType.User, "message")] UserEventRequest request,
271+
[WebPubSubTrigger("simplechat", WebPubSubEventType.User, "message")] UserEventRequest request,
268272
BinaryData data,
269273
WebPubSubDataType dataType,
270274
[WebPubSub(Hub = "simplechat")] IAsyncCollector<WebPubSubAction> actions)
271275
{
272276
await actions.AddAsync(WebPubSubAction.CreateSendToAllAction(
273-
BinaryData.FromString($"[{request.ConnectionContext.UserId}] {message.ToString()}"),
274-
dataType
275-
);
277+
BinaryData.FromString($"[{request.ConnectionContext.UserId}] {data.ToString()}"),
278+
dataType));
276279
return new UserEventResponse
277280
{
278281
Data = BinaryData.FromString("[SYSTEM] ack"),
279282
DataType = WebPubSubDataType.Text
280283
};
281284
}
282285
```
286+
- Add below `using` statements in header to resolve required dependencies.
287+
```c#
288+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
289+
using Microsoft.Azure.WebPubSub.Common;
290+
```
283291
284292
6. Add the client single page `index.html` in the project root folder and copy content as below.
285293
```html

articles/azure-web-pubsub/tutorial-serverless-notification.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ In this tutorial, you learn how to:
7575
```
7676
b. Run command to install specific function extension package.
7777
```bash
78-
func extensions install --package Microsoft.Azure.WebJobs.Extensions.WebPubSub --version 1.0.0
78+
func extensions install --package Microsoft.Azure.WebJobs.Extensions.WebPubSub --version 1.1.0
7979
```
8080

8181
3. Create an `index` function to read and host a static web page for clients.
@@ -204,6 +204,10 @@ In this tutorial, you learn how to:
204204
return connection;
205205
}
206206
```
207+
- Add below `using` statements in header to resolve required dependencies.
208+
```c#
209+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
210+
```
207211
208212
5. Create a `notification` function to generate notifications with `TimerTrigger`.
209213
```bash
@@ -265,6 +269,11 @@ In this tutorial, you learn how to:
265269
return value.ToString("0.000");
266270
}
267271
```
272+
- Add below `using` statements in header to resolve required dependencies.
273+
```c#
274+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
275+
using Microsoft.Azure.WebPubSub.Common;
276+
```
268277
269278
6. Add the client single page `index.html` in the project root folder and copy content as below.
270279
```html
@@ -310,14 +319,14 @@ In this tutorial, you learn how to:
310319
311320
:::image type="content" source="media/quickstart-serverless/copy-connection-string.png" alt-text="Screenshot of copying the Web PubSub connection string.":::
312321
313-
Run command below in the function folder to set the service connection string. Replace `<connection-string`> with your value as needed.
322+
Run command below in the function folder to set the service connection string. Replace `<connection-string>` with your value as needed.
314323
315324
```bash
316325
func settings add WebPubSubConnectionString "<connection-string>"
317326
```
318327
319328
> [!NOTE]
320-
> `TimerTrigger` used in the sample has dependency on Azure Storage, but you can use local storage emulator when the Function is running locally. If you got some error like `There was an error performing a read operation on the Blob Storage Secret Repository. Please ensure the 'AzureWebJobsStorage' connection string is valid.` You need to download and enable [Storage Emulator](../storage/common/storage-use-emulator.md).
329+
> `TimerTrigger` used in the sample has dependency on Azure Storage, but you can use local storage emulator when the Function is running locally. If you got some error like `There was an error performing a read operation on the Blob Storage Secret Repository. Please ensure the 'AzureWebJobsStorage' connection string is valid.`, you'll need to download and enable [Storage Emulator](../storage/common/storage-use-emulator.md).
321330
322331
Now you're able to run your local function by command below.
323332

0 commit comments

Comments
 (0)