You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-web-pubsub/howto-develop-eventhandler.md
+37-21Lines changed: 37 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,69 +5,85 @@ author: JialinXin
5
5
ms.author: jixin
6
6
ms.service: azure-web-pubsub
7
7
ms.topic: how-to
8
-
ms.date: 10/31/2021
8
+
ms.date: 01/27/2023
9
9
---
10
10
11
11
# Event handler in Azure Web PubSub service
12
12
13
-
The event handler handles the incoming client events. Event handlers are registered and configured in the service through portal or Azure CLI beforehand so that when a client event is triggered, the service can identify if the event is expected to be handled or not. We now support the event handler as the serverside, which exposes public accessible endpoint for the service to invoke when the event is triggered. In other words, it acts as a **webhook**.
13
+
The event handler handles the incoming client events. Event handlers are registered and configured in the service through the Azure portal or Azure CLI. When a client event is triggered, the service can send the event to the appropriate event handler. The Web PubSub service now supports the event handler as the server-side, which exposes the publicly accessible endpoint for the service to invoke when the event is triggered. In other words, it acts as a **webhook**.
14
14
15
-
Service delivers client events to the upstream webhook using the [CloudEvents HTTP protocol](https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md).
15
+
The Web PubSub service delivers client events to the upstream webhook using the [CloudEvents HTTP protocol](https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md).
16
16
17
-
For every event, it formulates an HTTP POST request to the registered upstream and expects an HTTP response.
17
+
For every event, the service formulates an HTTP POST request to the registered upstream endpoint and expects an HTTP response.
18
18
19
19
The data sending from the service to the server is always in CloudEvents `binary` format.
20
20
21
21
:::image type="content" source="media/howto-develop-eventhandler/event-trigger.png" alt-text="Screenshot of Web PubSub service event trigger.":::
22
22
23
23
## Upstream and Validation
24
24
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.
25
+
When you configure the webhook endpoint, the URL can include the `{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 `{event}`parameter can be useful when a PubSub WebSocket client sends custom events, that the event handler helps dispatch different events to different upstream endpoints. The `{event}` parameter isn't allowed in the URL domain name.
26
26
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 `*`.
27
+
When setting up the event handler webhook 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 `*`.
28
28
29
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.
30
30
31
-
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).
31
+
For now, we don't 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).
32
32
33
33
## Authentication between service and webhook
34
34
35
+
You can use any of these methods to authenticate between the service and webhook.
36
+
35
37
- Anonymous mode
36
38
- Simple Auth with `?code=<code>` is provided through the configured Webhook URL as query parameter.
37
-
- Use Azure Active Directory(Azure AD) authentication, check [here](howto-use-managed-identity.md) for details.
38
-
- Step1: Enable Identity for the Web PubSub service
39
-
- Step2: Select from existing AAD application that stands for your webhook web app
39
+
- Azure Active Directory(Azure AD) authentication. For more information, see [Use a managed identity in client events](howto-use-managed-identity.md#use-a-managed-identity-in-client-events-scenarios).
40
40
41
41
## Configure event handler
42
42
43
43
### Configure through Azure portal
44
44
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.
45
+
You can add an event handler to a new hub or edit an existing hub.
46
+
47
+
To configure an event handler in a new hub:
48
+
49
+
1. Go to your Azure Web PubSub service page in the **Azure portal**.
50
+
1. Select **Settings** from the menu.
51
+
1. Select **Add** to create a hub and configure your server-side webhook URL. Note: To add an event handler to an existing hub, select the hub and select **Edit**.
52
+
53
+
:::image type="content" source="media/quickstart-serverless/set-event-handler.png" alt-text="Screenshot of setting the event handler.":::
46
54
47
-
:::image type="content" source="media/quickstart-serverless/set-event-handler.png" alt-text="Screenshot of setting the event handler.":::
55
+
1. Enter your hub name.
56
+
1. Select **Add** under **Configure Even Handlers**.
57
+
1. In the event handler page, configure the following fields:
58
+
1. Enter the server webhook URL in the **URL Template** field.
59
+
1. Select the **System events** that you want to subscribe to.
60
+
1. Select the **User events** that you want to subscribe to.
61
+
1. Select **Authentication** method to authenticate upstream requests.
62
+
1. Select **Confirm**.
63
+
:::image type="content" source="media/howto-develop-eventhandler/configure-event-handler.png" alt-text="Screenshot of Azure Web PubSub Configure Event Handler.":::
48
64
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.
65
+
1. Select **Save** at the top of the **Configure Hub Settings** page.
50
66
51
-
:::image type="content" source="media/quickstart-serverless/edit-event-handler.png" alt-text="Screenshot of editing the event handler.":::
67
+
:::image type="content" source="media/quickstart-serverless/edit-event-handler.png" alt-text="Screenshot of Azure Web PubSub Configure Hub Settings.":::
52
68
53
69
### Configure through Azure CLI
54
70
55
71
Use the Azure CLI [**az webpubsub hub**](/cli/azure/webpubsub/hub) group commands to configure the event handler settings.
56
72
57
73
Commands | Description
58
74
--|--
59
-
create | Create hub settings for WebPubSub Service.
60
-
delete | Delete hub settings for WebPubSub Service.
61
-
list | List all hub settings for WebPubSub Service.
62
-
show | Show hub settings for WebPubSub Service.
63
-
update | Update hub settings for WebPubSub Service.
75
+
`create` | Create hub settings for WebPubSub Service.
76
+
`delete` | Delete hub settings for WebPubSub Service.
77
+
`list` | List all hub settings for WebPubSub Service.
78
+
`show` | Show hub settings for WebPubSub Service.
79
+
`update` | Update hub settings for WebPubSub Service.
64
80
65
-
Below is an example of creating 2 webhook URLs for hub `MyHub` of `MyWebPubSub` resource.
81
+
Here's an example of creating two webhook URLs for hub `MyHub` of `MyWebPubSub` resource:
0 commit comments