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
@@ -647,20 +658,20 @@ For now, you need to implement the event handler by your own in Java. The steps
647
658
2. First we'd like to handle the abuse protection OPTIONS requests, we check if the header contains `WebHook-Request-Origin` header, and we return the header `WebHook-Allowed-Origin`. For simplicity for demo purpose, we return `*` to allow all the origins.
3. Then we'd like to check if the incoming requests are the events we expect. Let's say we now care about the system `connected` event, which should contain the header `ce-type` as `azure.webpubsub.sys.connected`. We add the logic after abuse protection to broadcast the connected event to all clients so they can see who joined the chat room.
if ("azure.webpubsub.sys.connected".equals(event)) {
@@ -677,7 +688,7 @@ For now, you need to implement the event handler by your own in Java. The steps
677
688
4. The `ce-type` of `message` event is always `azure.webpubsub.user.message`. Details see [Event message](./reference-cloud-events.md#message). We update the logic to handle messages that when a message comes in we broadcast the message in JSON format to all the connected clients.
@@ -700,7 +716,7 @@ For now, you need to implement the event handler by your own in Python. The step
700
716
701
717
2. First we'd like to handle the abuse protection OPTIONS requests, we check if the header contains `WebHook-Request-Origin` header, and we return the header `WebHook-Allowed-Origin`. For simplicity for demo purpose, we return `*` to allow all the origins.
@@ -713,8 +729,8 @@ For now, you need to implement the event handler by your own in Python. The step
713
729
714
730
3. Then we'd like to check if the incoming requests are the events we expect. Let's say we now care about the system `connected` event, which should contain the header `ce-type` as `azure.webpubsub.sys.connected`. We add the logic after abuse protection:
@@ -823,7 +839,7 @@ In this section, we use Azure CLI to set the event handlers and use [awps-tunnel
823
839
824
840
We set the URL template to use `tunnel` scheme so that Web PubSub routes messages through the `awps-tunnel`'s tunnel connection. Event handlers can be set from either the portal or the CLI as [described in this article](howto-develop-eventhandler.md#configure-event-handler), here we set it through CLI. Since we listen events in path `/eventhandler` as the previous step sets, we set the url template to `tunnel:///eventhandler`.
825
841
826
-
Use the Azure CLI [az webpubsub hub create](/cli/azure/webpubsub/hub#az-webpubsub-hub-update) command to create the event handler settings for the chat hub.
842
+
Use the Azure CLI [az webpubsub hub create](/cli/azure/webpubsub/hub#az-webpubsub-hub-create) command to create the event handler settings for the `Sample_ChatApp` hub.
827
843
828
844
> [!Important]
829
845
> Replace <your-unique-resource-name> with the name of your Web PubSub resource created from the previous steps.
@@ -884,6 +900,255 @@ Open `http://localhost:8080/index.html`. You can input your user name and start
884
900
885
901
<!-- Adding Lazy Auth part with `connect` handling -->
886
902
903
+
## Lazy Auth with `connect` event handler
904
+
905
+
In previous sections, we demonstrate how to use [negotiate](#add-negotiate-endpoint) endpoint to return the Web PubSub service URL and the JWT access token for the clients to connect to Web PubSub service. In some cases, for example, edge devices that have limited resources, clients might prefer direct connect to Web PubSub resources. In such cases, you can configure `connect` event handler to lazy auth the clients, assign user ID to the clients, specify the groups the clients join once they connect, configure the permissions the clients have and WebSocket subprotocol as the WebSocket response to the client, etc. Details please refer to [connect event handler spec](./reference-cloud-events.md#connect).
906
+
907
+
Now let's use `connect` event handler to acheive the similar as what the [negotiate](#add-negotiate-endpoint) section does.
908
+
909
+
### Update hub settings
910
+
911
+
First let's update hub settings to also include `connect` event handler, we need to also allow anonymous connect so that clients without JWT access token can connect to the service.
912
+
913
+
Use the Azure CLI [az webpubsub hub update](/cli/azure/webpubsub/hub#az-webpubsub-hub-update) command to create the event handler settings for the `Sample_ChatApp` hub.
914
+
915
+
> [!Important]
916
+
> Replace <your-unique-resource-name> with the name of your Web PubSub resource created from the previous steps.
Now let's update upstream logic to handle connect event. We could also remove the negotiate endpoint now.
925
+
926
+
As similar to what we do in negotiate endpoint as demo purpose, we also read id from the query parameters. In connect event, the original client query is preserved in connect event requet body.
927
+
928
+
# [C#](#tab/csharp)
929
+
930
+
Inside the class `Sample_ChatApp`, override `OnConnectAsync()` to handle `connect` event:
Now let's handle the system `connect` event, which should contain the header `ce-type` as `azure.webpubsub.sys.connect`. We add the logic after abuse protection:
Now let's update the web page to direct connect to Web PubSub service. One thing to mention is that now fordemo purpose the Web PubSub service endpoint is hard-coded into the client code, please update the service hostname `<the host name of your service>`in the below html with the value from your own service. It might be still useful to fetch the Web PubSub service endpoint value from your server, it gives you more flexibility and controllability to where the client connects to.
1111
+
1112
+
```html
1113
+
<html>
1114
+
<body>
1115
+
<h1>Azure Web PubSub Chat</h1>
1116
+
<input id="message" placeholder="Type to chat...">
1117
+
<div id="messages"></div>
1118
+
<script>
1119
+
(async function() {
1120
+
// sample host: mock.webpubsub.azure.com
1121
+
let hostname = "<the host name of your service>";
1122
+
let id = prompt('Please input your user name');
1123
+
let ws = new WebSocket(`wss://${hostname}/client/hubs/Sample_ChatApp?id=${id}`);
1124
+
ws.onopen = () => console.log('connected');
1125
+
1126
+
let messages = document.querySelector('#messages');
Now [rerun the server](#run-the-web-server) and visit the web page following the instructions before. If you've stopped `awps-tunnel`, please also [rerun the tunnel tool](#run-awps-tunnel-locally).
1151
+
887
1152
## Next steps
888
1153
889
1154
This tutorial provides you with a basic idea of how the event system works in Azure Web PubSub service.
0 commit comments