Skip to content

Commit b845a03

Browse files
authored
Merge pull request #231141 from cebundy/wps-qs-client-sdk
[Web PubSub] Review client SDK quickstart
2 parents ed7327f + fb43f3a commit b845a03

File tree

1 file changed

+135
-33
lines changed

1 file changed

+135
-33
lines changed

articles/azure-web-pubsub/quickstart-use-client-sdk.md

Lines changed: 135 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,75 @@
11
---
2-
title: Quickstart - Pub-sub using Azure Web PubSub client SDK
2+
title: Quickstart - Create a client using the Azure Web PubSub client SDK (preview)
33
description: Quickstart showing how to use the Azure Web PubSub client SDK
44
author: zackliu
55
ms.author: chenyl
66
ms.service: azure-web-pubsub
77
ms.topic: quickstart
8-
ms.date: 02/7/2023
8+
ms.date: 03/15/2023
99
ms.custom: mode-api, devx-track-azurecli
1010
ms.devlang: azurecli
1111
---
1212

13-
# Quickstart: Pub-sub using Web PubSub client SDK
13+
# Quickstart: Create a client using the Azure Web PubSub client SDK (preview)
14+
15+
Get started with the Azure Web PubSub client SDK for .NET or JavaScript to create a Web PubSub client
16+
that:
17+
18+
* connects to a Web PubSub service instance
19+
* subscribes a Web PubSub group.
20+
* publishes a message to the Web PubSub group.
21+
22+
[API reference documentation](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/web-pubsub/web-pubsub-client) | [Library source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/web-pubsub/web-pubsub-client/src) | [Package (JavaScript npm)](https://www.npmjs.com/package/@azure/web-pubsub-client) | [Samples](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub-client/samples-dev/helloworld.ts)
23+
24+
[API reference documentation](https://github.com/Azure/azure-sdk-for-net#azure-sdk-for-net) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src) | [Package (NuGet)](https://www.nuget.org/packages/Azure.Messaging.WebPubSub.Client) | [Samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/samples)
1425

15-
This quickstart guide demonstrates how to construct a project using the Web PubSub client SDK, connect to the Web PubSub, subscribe to messages from groups and publish a message to the group.
1626

1727
> [!NOTE]
18-
> The client SDK is still in preview version. The interface may change in later versions
28+
> The client SDK is still in preview version. The interface may change in later versions.
1929
2030
## Prerequisites
2131

22-
- 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)
32+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2333
- A file editor such as Visual Studio Code.
2434

25-
Install the dependencies for the language you're using:
35+
## Setting up
36+
37+
### Create an Azure Web PubSub service instance
38+
39+
1. In the Azure portal **Home** page, select **Create a resource**.
40+
1. In the **Search the Marketplace** box, enter *Web PubSub*.
41+
1. Select **Web PubSub** from the results.
42+
1. Select **Create**.
43+
1. Create a new resource group
44+
1. Select **Create new**.
45+
1. Enter the name and select **OK**.
46+
1. Enter a **Resource Name** for the service instance.
47+
1. Select **Pricing tier**. You can choose **Free** for testing.
48+
1. Select **Create**, then **Create** again to confirm the new service instance.
49+
1. Once deployment is complete, select **Go to resource**.
50+
51+
### Generate the client URL
52+
53+
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>`.
54+
55+
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.
56+
57+
1. In the Azure portal, go to your Web PubSub service resource page.
58+
1. Select **Keys** from the menu.
59+
1. In the **Client URL Generator** section:
60+
1. Select **Send To Groups**
61+
1. Select **Allow Sending To Specific Groups**.
62+
1. Enter *group1* in the **Group Name** field and select **Add**.
63+
1. Select **Join/Leave Groups**.
64+
1. Select **Allow Joining/Leaving Specific Groups**.
65+
1. Enter *group1* in the **Group Name** field and select **Add**.
66+
1. Copy and save the **Client Access URL** for use later in this article.
67+
68+
:::image type="content" source="media/howto-websocket-connect/generate-client-url.png" alt-text="Screenshot of the Web PubSub Client URL Generator.":::
69+
70+
### Install programming language
71+
72+
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.
2673

2774
# [JavaScript](#tab/javascript)
2875

@@ -38,67 +85,93 @@ Install both the .NET Core SDK and dotnet runtime.
3885

3986
---
4087

41-
## Add the Web PubSub client SDK
88+
### Install the package
89+
90+
Install the Azure Web PubSub client SDK for the language you're using.
4291

4392
# [JavaScript](#tab/javascript)
4493

45-
The SDK is available as an [npm module](https://www.npmjs.com/package/@azure/web-pubsub-client)
94+
The SDK is available as an [npm module](https://www.npmjs.com/package/@azure/web-pubsub-client).
95+
96+
Open a terminal window and install the Web PubSub client SDK using the following command.
4697

4798
```bash
4899
npm install @azure/web-pubsub-client
49100
```
50101

102+
Note that the SDK is available as an [npm module](https://www.npmjs.com/package/@azure/web-pubsub-client).
103+
51104
# [C#](#tab/csharp)
52105

53-
The SDK is available as an [NuGet packet](https://www.nuget.org/packages/Azure.Messaging.WebPubSub.Client)
106+
Open a terminal window to create your project and install the Web PubSub client SDK.
54107

55108
```bash
109+
# create project directory
110+
mkdir webpubsub-client
111+
112+
# change to the project directory
113+
cd webpubsub-client
114+
56115
# Add a new .net project
57116
dotnet new console
58117

59118
# Add the client SDK
60119
dotnet add package Azure.Messaging.WebPubSub.Client --prerelease
61120
```
62121

122+
Note that the SDK is available as a [NuGet packet](https://www.nuget.org/packages/Azure.Messaging.WebPubSub.Client).
123+
63124
---
64125

65-
## Connect to Web PubSub
126+
## Code examples
66127

67-
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>`. A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown as the following diagram.
68128

69-
![The diagram shows how to get client access url.](./media/howto-websocket-connect/generate-client-url.png)
129+
### Create and connect to the Web PubSub service
70130

71-
As shown in the diagram above, the client has the permissions to send messages to and join a specific group named `group1`.
131+
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 URL in your code. In the production world, we usually set up an app server to return this URL on demand.
72132

133+
For this example, you can use the Client Access URL you generated in the portal.
73134

74135
# [JavaScript](#tab/javascript)
75136

76-
Add a file with name `index.js` and add following codes:
137+
In the terminal window, create a new directory for your project and change to that directory.
138+
139+
```bash
140+
mkdir webpubsub-client
141+
cd webpubsub-client
142+
```
143+
144+
Create a file with name `index.js` and enter following code:
77145

78146
```javascript
79147
const { WebPubSubClient } = require("@azure/web-pubsub-client");
80-
// Instantiates the client object. <client-access-url> is copied from Azure portal mentioned above.
81-
const client = new WebPubSubClient("<client-access-url>");
148+
// Instantiates the client object. env.process.env.WebPubSubClientURL
149+
// env.process.env.WebPubSubClientURL is the Client Access URL from Azure portal
150+
const client = new WebPubSubClient(env.process.env.WebPubSubClientURL);
82151
```
83152

84153
# [C#](#tab/csharp)
85154

86-
Edit the `Program.cs` file and add following codes:
155+
Edit the `Program.cs` file and add following code:
87156

88157
```csharp
89158
using Azure.Messaging.WebPubSub.Clients;
90-
// Instantiates the client object. <client-access-uri> is copied from Azure portal mentioned above.
91-
var client = new WebPubSubClient(new Uri("<client-access-uri>"));
159+
// Client Access URL from Azure portal
160+
var clientURL = Environment.GetEnvironmentVariable("WebPubSubClientURL"));
161+
// Instantiates the client object.
162+
var client = new WebPubSubClient(new Uri(clientURL));
92163
```
93164

94165
---
95166

96-
## Subscribe to a group
167+
### Subscribe to a group
97168

98-
To receive message from groups, you need to add a callback to handle messages you receive from the group, and you must join the group before you can receive messages from it. The following code subscribes the client to a group called `group1`.
169+
To receive message from a group, you need to subscribe to the group and add a callback to handle messages you receive from the group. The following code subscribes the client to a group called `group1`.
99170

100171
# [JavaScript](#tab/javascript)
101172

173+
Add this following code to the `index.js` file:
174+
102175
```javascript
103176
// callback to group messages.
104177
client.on("group-message", (e) => {
@@ -114,6 +187,8 @@ client.joinGroup("group1");
114187

115188
# [C#](#tab/csharp)
116189

190+
Add the following code to the `Program.cs` file:
191+
117192
```csharp
118193
// callback to group messages.
119194
client.GroupMessageReceived += eventArgs =>
@@ -128,27 +203,62 @@ await client.StartAsync();
128203
// join a group to subscribe message from the group
129204
await client.JoinGroupAsync("group1");
130205
```
206+
131207
---
132208

133-
## Publish a message to a group
209+
### Publish a message to a group
134210

135-
Then you can send messages to the group and as the client has joined the group before, you can receive the message you've sent.
211+
After your client has subscribed to the group, it can send messages to and receive the message from the group.
136212

137213
# [JavaScript](#tab/javascript)
138214

215+
Add the following code to the `index.js` file:
216+
139217
```javascript
140218
client.sendToGroup("group1", "Hello World", "text");
141219
```
142220

143221
# [C#](#tab/csharp)
144222

223+
Add the following code to the `Program.cs` file:
224+
145225
```csharp
146226
await client.SendToGroupAsync("group1", BinaryData.FromString("Hello World"), WebPubSubDataType.Text);
147227
```
148228

149229
---
150230

151-
## Repository and Samples
231+
## Run the code
232+
233+
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 can see the message you sent from the second client in the first client's terminal window.
234+
235+
# [JavaScript](#tab/javascript)
236+
237+
To start the client go the terminal and run the following command. Replace the `<Client Access URL>` with the client access URL you copied from the portal.
238+
239+
```bash
240+
export WebPubSubClientURL="<Client Access URL>"
241+
node index.js
242+
```
243+
244+
# [C#](#tab/csharp)
245+
246+
To start the client, run the following command in your terminal replacing the `<client-access-url>` with the client access URL you copied from the portal:
247+
248+
```bash
249+
export WebPubSubClientURL="<Client Access URL>"
250+
dotnet run <client-access-url>
251+
```
252+
253+
---
254+
255+
## Clean up resources
256+
257+
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**.
258+
259+
## Next steps
260+
261+
To learn more the Web PubSub service client SDKs, see the following resources:
152262

153263
# [JavaScript](#tab/javascript)
154264

@@ -165,11 +275,3 @@ await client.SendToGroupAsync("group1", BinaryData.FromString("Hello World"), We
165275
[.NET SDK repository on GitHub](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/webpubsub/Azure.Messaging.WebPubSub.Client)
166276

167277
[Log streaming sample](https://github.com/Azure/azure-webpubsub/tree/main/samples/csharp/logstream/sdk)
168-
169-
---
170-
171-
## Next steps
172-
173-
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.
174-
175-
[!INCLUDE [next step](includes/include-next-step.md)]

0 commit comments

Comments
 (0)