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/tutorial-build-chat.md
+224-1Lines changed: 224 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -823,7 +823,7 @@ In this section, we use Azure CLI to set the event handlers and use [awps-tunnel
823
823
824
824
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
825
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.
826
+
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
827
828
828
> [!Important]
829
829
> Replace <your-unique-resource-name> with the name of your Web PubSub resource created from the previous steps.
@@ -884,6 +884,229 @@ Open `http://localhost:8080/index.html`. You can input your user name and start
884
884
885
885
<!-- Adding Lazy Auth part with `connect` handling -->
886
886
887
+
## Lazy Auth with `connect` event handler
888
+
889
+
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-client-events.md#connect). Now let's use `connect` event handler to acheive the similar as what the [negotiate](#negotiate) section does.
890
+
891
+
### Update hub settings
892
+
893
+
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.
894
+
895
+
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.
896
+
897
+
> [!Important]
898
+
> 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. As similar to what we doin 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.
907
+
908
+
# [C#](#tab/csharp)
909
+
910
+
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 for demo 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.
1070
+
1071
+
```html
1072
+
<html>
1073
+
<body>
1074
+
<h1>Azure Web PubSub Chat</h1>
1075
+
<input id="message" placeholder="Type to chat...">
1076
+
<div id="messages"></div>
1077
+
<script>
1078
+
(async function () {
1079
+
let hostname = "<the host name of your service>";
1080
+
let id = prompt('Please input your user name');
1081
+
let ws = new WebSocket(`wss://${hostname}/client/hubs/Sample_ChatApp?id=${id}`);
1082
+
ws.onopen = () => console.log('connected');
1083
+
1084
+
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.
1109
+
887
1110
## Next steps
888
1111
889
1112
This tutorial provides you with a basic idea of how the event system works in Azure Web PubSub service.
0 commit comments