|
| 1 | +--- |
| 2 | +title: How to generate client access URL for Azure Web PubSub clients |
| 3 | +description: How to generate client access URL for Azure Web PubSub clients. |
| 4 | +author: vicancy |
| 5 | +ms.author: lianwei |
| 6 | +ms.date: 04/25/2023 |
| 7 | +ms.service: azure-web-pubsub |
| 8 | +ms.topic: how-to |
| 9 | +--- |
| 10 | + |
| 11 | +# How to generate client access URL for the clients |
| 12 | + |
| 13 | +A client, be it a browser 💻, a mobile app 📱, or an IoT device 💡, uses a **Client Access URL** to connect and authenticate with your resource. This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. This article shows you several ways to get the Client Access URL. |
| 14 | + |
| 15 | +- For quick start, copy one from the Azure portal |
| 16 | +- For development, generate the value using [Web PubSub server SDK](./reference-server-sdk-js.md) |
| 17 | +- If you're using Azure AD, you can also invoke the [Generate Client Token REST API](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token) |
| 18 | + |
| 19 | +## Copy from the Azure portal |
| 20 | +In the Keys tab in Azure portal, there's a Client URL Generator tool to quickly generate a Client Access URL for you, as shown in the following diagram. Values input here aren't stored. |
| 21 | + |
| 22 | +:::image type="content" source="./media/howto-websocket-connect/generate-client-url.png" alt-text="Screenshot of the Web PubSub Client URL Generator."::: |
| 23 | + |
| 24 | +## Generate from service SDK |
| 25 | +The same Client Access URL can be generated by using the Web PubSub server SDK. |
| 26 | + |
| 27 | +# [JavaScript](#tab/javascript) |
| 28 | + |
| 29 | +1. Follow [Getting started with server SDK](./reference-server-sdk-js.md#getting-started) to create a `WebPubSubServiceClient` object `service` |
| 30 | + |
| 31 | +2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`: |
| 32 | + * Configure user ID |
| 33 | + ```js |
| 34 | + let token = await serviceClient.getClientAccessToken({ userId: "user1" }); |
| 35 | + ``` |
| 36 | + * Configure the lifetime of the token |
| 37 | + ```js |
| 38 | + let token = await serviceClient.getClientAccessToken({ expirationTimeInMinutes: 5 }); |
| 39 | + ``` |
| 40 | + * Configure a role that can join group `group1` directly when it connects using this Client Access URL |
| 41 | + ```js |
| 42 | + let token = await serviceClient.getClientAccessToken({ roles: ["webpubsub.joinLeaveGroup.group1"] }); |
| 43 | + ``` |
| 44 | + * Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL |
| 45 | + ```js |
| 46 | + let token = await serviceClient.getClientAccessToken({ roles: ["webpubsub.sendToGroup.group1"] }); |
| 47 | + ``` |
| 48 | + * Configure a group `group1` that the client joins once it connects using this Client Access URL |
| 49 | + ```js |
| 50 | + let token = await serviceClient.getClientAccessToken({ groups: ["group1"] }); |
| 51 | + ``` |
| 52 | + |
| 53 | +# [C#](#tab/csharp) |
| 54 | + |
| 55 | +1. Follow [Getting started with server SDK](./reference-server-sdk-csharp.md#getting-started) to create a `WebPubSubServiceClient` object `service` |
| 56 | + |
| 57 | +2. Generate Client Access URL by calling `WebPubSubServiceClient.GetClientAccessUri`: |
| 58 | + * Configure user ID |
| 59 | + ```csharp |
| 60 | + var url = service.GetClientAccessUri(userId: "user1"); |
| 61 | + ``` |
| 62 | + * Configure the lifetime of the token |
| 63 | + ```csharp |
| 64 | + var url = service.GetClientAccessUri(expiresAfter: TimeSpan.FromMinutes(5)); |
| 65 | + ``` |
| 66 | + * Configure a role that can join group `group1` directly when it connects using this Client Access URL |
| 67 | + ```csharp |
| 68 | + var url = service.GetClientAccessUri(roles: new string[] { "webpubsub.joinLeaveGroup.group1" }); |
| 69 | + ``` |
| 70 | + * Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL |
| 71 | + ```csharp |
| 72 | + var url = service.GetClientAccessUri(roles: new string[] { "webpubsub.sendToGroup.group1" }); |
| 73 | + ``` |
| 74 | + * Configure a group `group1` that the client joins once it connects using this Client Access URL |
| 75 | + ```csharp |
| 76 | + var url = service.GetClientAccessUri(groups: new string[] { "group1" }); |
| 77 | + ``` |
| 78 | + |
| 79 | +# [Python](#tab/python) |
| 80 | + |
| 81 | +1. Follow [Getting started with server SDK](./reference-server-sdk-python.md#install-the-package) to create a `WebPubSubServiceClient` object `service` |
| 82 | + |
| 83 | +2. Generate Client Access URL by calling `WebPubSubServiceClient.get_client_access_token`: |
| 84 | + * Configure user ID |
| 85 | + ```python |
| 86 | + token = service.get_client_access_token(user_id="user1") |
| 87 | + ``` |
| 88 | + * Configure the lifetime of the token |
| 89 | + ```python |
| 90 | + token = service.get_client_access_token(minutes_to_expire=5) |
| 91 | + ``` |
| 92 | + * Configure a role that can join group `group1` directly when it connects using this Client Access URL |
| 93 | + ```python |
| 94 | + token = service.get_client_access_token(roles=["webpubsub.joinLeaveGroup.group1"]) |
| 95 | + ``` |
| 96 | + * Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL |
| 97 | + ```python |
| 98 | + token = service.get_client_access_token(roles=["webpubsub.sendToGroup.group1"]) |
| 99 | + ``` |
| 100 | + * Configure a group `group1` that the client joins once it connects using this Client Access URL |
| 101 | + ```python |
| 102 | + token = service.get_client_access_token(groups=["group1"]) |
| 103 | + ``` |
| 104 | + |
| 105 | +# [Java](#tab/java) |
| 106 | + |
| 107 | +1. Follow [Getting started with server SDK](./reference-server-sdk-java.md#getting-started) to create a `WebPubSubServiceClient` object `service` |
| 108 | + |
| 109 | +2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`: |
| 110 | + * Configure user ID |
| 111 | + ```java |
| 112 | + GetClientAccessTokenOptions option = new GetClientAccessTokenOptions(); |
| 113 | + option.setUserId(id); |
| 114 | + WebPubSubClientAccessToken token = service.getClientAccessToken(option); |
| 115 | + ``` |
| 116 | + * Configure the lifetime of the token |
| 117 | + ```java |
| 118 | + GetClientAccessTokenOptions option = new GetClientAccessTokenOptions(); |
| 119 | + option.setExpiresAfter(Duration.ofDays(1)); |
| 120 | + WebPubSubClientAccessToken token = service.getClientAccessToken(option); |
| 121 | + ``` |
| 122 | + * Configure a role that can join group `group1` directly when it connects using this Client Access URL |
| 123 | + ```java |
| 124 | + GetClientAccessTokenOptions option = new GetClientAccessTokenOptions(); |
| 125 | + option.addRole("webpubsub.joinLeaveGroup.group1"); |
| 126 | + WebPubSubClientAccessToken token = service.getClientAccessToken(option); |
| 127 | + ``` |
| 128 | + * Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL |
| 129 | + ```java |
| 130 | + GetClientAccessTokenOptions option = new GetClientAccessTokenOptions(); |
| 131 | + option.addRole("webpubsub.sendToGroup.group1"); |
| 132 | + WebPubSubClientAccessToken token = service.getClientAccessToken(option); |
| 133 | + ``` |
| 134 | + * Configure a group `group1` that the client joins once it connects using this Client Access URL |
| 135 | + ```java |
| 136 | + GetClientAccessTokenOptions option = new GetClientAccessTokenOptions(); |
| 137 | + option.setGroups(Arrays.asList("group1")), |
| 138 | + WebPubSubClientAccessToken token = service.getClientAccessToken(option); |
| 139 | + ``` |
| 140 | +--- |
| 141 | + |
| 142 | +In real-world code, we usually have a server side to host the logic generating the Client Access URL. When a client request comes in, the server side can use the general authentication/authorization workflow to validate the client request. Only valid client requests can get the Client Access URL back. |
| 143 | + |
| 144 | +## Invoke the Generate Client Token REST API |
| 145 | + |
| 146 | +You can enable Azure AD in your service and use the Azure AD token to invoke [Generate Client Token rest API](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token) to get the token for the client to use. |
| 147 | + |
| 148 | +1. Follow [Authorize from application](./howto-authorize-from-application.md) to enable Azure AD. |
| 149 | +2. Follow [Get Azure AD token](./howto-authorize-from-application.md#use-postman-to-get-the-azure-ad-token) to get the Azure AD token with Postman. |
| 150 | +3. Use the Azure AD token to invoke `:generateToken` with Postman: |
| 151 | + 1. For the URI, enter `https://{Endpoint}/api/hubs/{hub}/:generateToken?api-version=2022-11-01` |
| 152 | + 2. On the **Auth** tab, select **Bearer Token** and paste the Azure AD token fetched in the previous step |
| 153 | + 3. Select **Send** and you see the Client Access Token in the response: |
| 154 | + ```json |
| 155 | + { |
| 156 | + "token": "ABCDEFG.ABC.ABC" |
| 157 | + } |
| 158 | + ``` |
| 159 | +4. The Client Access URI is in the format of `wss://<endpoint>/client/hubs/<hub_name>?access_token=<token>` |
| 160 | + |
0 commit comments