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/quickstart-use-client-sdk.md
+93-32Lines changed: 93 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,11 +29,48 @@ Get started with the Azure Web PubSub client SDK for Python or JavaScript to cre
29
29
30
30
## Prerequisites
31
31
32
-
- An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F)
32
+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
33
33
- A Web PubSub instance. If you haven't created one, you can follow the guidance: [Create a Web PubSub instance from Azure portal](./howto-develop-create-instance.md)
34
34
- A file editor such as Visual Studio Code.
35
35
36
-
Install the dependencies for the language you're using:
36
+
## Setting up
37
+
38
+
### Create an Azure Web PubSub service instance
39
+
40
+
1. In the Azure portal and from **Home**, select **Create a resource**.
41
+
1. In the **Search the Marketplace** box, enter *Web PubSub*.
42
+
1. Select **Web PubSub** from the results.
43
+
1. Select **Create**.
44
+
1. Create a new resource group
45
+
1. Select **Create new**.
46
+
1. Enter the name and select **OK**.
47
+
1. Enter a **Resource Name** for the service instance.
48
+
1. Select **Pricing tier**. You can choose **Free** for testing.
49
+
1. Select **Create**, then **Create** again to confirm the new service instance.
50
+
1. Select **Go to resource** to go to the service instance when the deployment is complete.
51
+
52
+
### Generate the client URL
53
+
54
+
A client uses a Client Access URL to connect and authenticate with the service, which follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`.
55
+
56
+
To give the client permission to send messages to and join a specific group, you must generate a Client Access URL with the **Send To Groups** and **Join/Leave Groups** permissions.
57
+
58
+
1. In the Azure portal, go to your Web PubSub service resource page.
59
+
1. Select **Keys** from the menu.
60
+
1. In the **Client URL Generator** section:
61
+
1. Select **Send To Groups**
62
+
1. Select **Allow Sending To Specific Groups**.
63
+
1. Enter *group1* in the **Group Name** field and select **Add**.
64
+
1. Select **Join/Leave Groups**.
65
+
1. Select **Allow Joining/Leaving Specific Groups**.
66
+
1. Enter *group1* in the **Group Name** field and select **Add**.
67
+
1. Copy and save the **Client Access URL** for use later in this article.
68
+
69
+
:::image type="content" source="media/howto-websocket-connect/generate-client-url.png" alt-text="Screenshot of the Web PubSub Client URL Generator.":::
70
+
71
+
### Install programming language
72
+
73
+
This quickstart uses the Azure Web PubSub client SDK for JavaScript or C#. Open a terminal window and install the dependencies for the language you're using.
37
74
38
75
# [JavaScript](#tab/javascript)
39
76
@@ -49,7 +86,7 @@ Install both the .NET Core SDK and dotnet runtime.
A client uses a Client Access URL to connect and authenticate with the service, which follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`.
113
+
## Code examples
79
114
80
-
1. In the Azure portal, go to your Web PubSub service resource page.
81
-
1. Select **Keys** from the menu.
82
-
1. In the **Client URL Generator** section:
83
-
1. Select **Send To Groups**
84
-
1. Select **Allow Sending To Specific Groups**.
85
-
1. Enter *group1* in the **Group Name** field and select **Add**.
86
-
1. Select **Join/Leave Groups**.
87
-
1. Select **Allow Joining/Leaving Specific Groups**.
88
-
1. Enter *group1* in the **Group Name** field and select **Add**.
89
-
1. Copy the **Client Access URL**.
115
+
From your terminal window, create a new directory and navigate to it.
90
116
91
-
:::image type="content" source="media/howto-websocket-connect/generate-client-url.png" alt-text="Screenshot of the Web PubSub Client URL Generator.":::
117
+
### Create and connect to the Web PubSub service
92
118
93
-
As shown above, the client has the permissions to send messages to and join a specific group named `group1`.
119
+
This code example creates a Web PubSub client that connects to the Web PubSub service instance. A client uses a Client Access URL to connect and authenticate with the service. It's best practice to not hard code the Client Access in your code.
94
120
121
+
For this example, you can use the Client Access URL you generated in the portal.
95
122
96
123
# [JavaScript](#tab/javascript)
97
124
98
-
Add a file with name `index.js` and add following code:
125
+
Create a file with name `index.js` and enter following code:
Run the client in your terminal. To verify the client is sending and receiving messages, you can open a second terminal and start the client from the same directory. You see the message you sent in the first terminal window.
214
+
215
+
# [JavaScript](#tab/javascript)
216
+
217
+
Add this code to the `index.js` file:
218
+
219
+
```bash
220
+
export WebPubSubClientURL="<Client Access URL>"
221
+
node index.js
222
+
```
223
+
224
+
# [C#](#tab/csharp)
225
+
226
+
Copy the Client Access URL from the portal and run the following command in your terminal replacing the `<client-access-url>` with the Client Access URL you copied from the portal:
227
+
228
+
```bash
229
+
dotnet run <client-access-url>
230
+
```
231
+
232
+
---
233
+
234
+
## Clean up resources
235
+
236
+
To delete the resources you created in this quickstart, you can delete the resource group you created. Go to the Azure portal, select your resource group, and select **Delete resource group**.
237
+
238
+
## Next steps
239
+
240
+
This quickstart provides you with a basic idea of how to connect to the Web PubSub with client SDK and how to subscribe to group messages and publish messages to groups.
241
+
242
+
To learn more the Web PubSub service SDKs, see the following resources:
174
243
175
244
# [JavaScript](#tab/javascript)
176
245
@@ -187,11 +256,3 @@ await client.SendToGroupAsync("group1", BinaryData.FromString("Hello World"), We
187
256
[.NET SDK repository on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/webpubsub/Azure.Messaging.WebPubSub.Client)
This quickstart provides you with a basic idea of how to connect to the Web PubSub with client SDK and how to subscribe to group messages and publish messages to groups.
0 commit comments