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
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.
16
26
17
27
> [!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.
19
29
20
30
## Prerequisites
21
31
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).
23
33
- A file editor such as Visual Studio Code.
24
34
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.
26
73
27
74
# [JavaScript](#tab/javascript)
28
75
@@ -38,67 +85,93 @@ Install both the .NET Core SDK and dotnet runtime.
38
85
39
86
---
40
87
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.
42
91
43
92
# [JavaScript](#tab/javascript)
44
93
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.
46
97
47
98
```bash
48
99
npm install @azure/web-pubsub-client
49
100
```
50
101
102
+
Note that the SDK is available as an [npm module](https://www.npmjs.com/package/@azure/web-pubsub-client).
103
+
51
104
# [C#](#tab/csharp)
52
105
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.
Note that the SDK is available as a [NuGet packet](https://www.nuget.org/packages/Azure.Messaging.WebPubSub.Client).
123
+
63
124
---
64
125
65
-
## Connect to Web PubSub
126
+
## Code examples
66
127
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.
68
128
69
-

129
+
### Create and connect to the Web PubSub service
70
130
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.
72
132
133
+
For this example, you can use the Client Access URL you generated in the portal.
73
134
74
135
# [JavaScript](#tab/javascript)
75
136
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:
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`.
99
170
100
171
# [JavaScript](#tab/javascript)
101
172
173
+
Add this following code to the `index.js` file:
174
+
102
175
```javascript
103
176
// callback to group messages.
104
177
client.on("group-message", (e) => {
@@ -114,6 +187,8 @@ client.joinGroup("group1");
114
187
115
188
# [C#](#tab/csharp)
116
189
190
+
Add the following code to the `Program.cs` file:
191
+
117
192
```csharp
118
193
// callback to group messages.
119
194
client.GroupMessageReceived+=eventArgs=>
@@ -128,27 +203,62 @@ await client.StartAsync();
128
203
// join a group to subscribe message from the group
129
204
awaitclient.JoinGroupAsync("group1");
130
205
```
206
+
131
207
---
132
208
133
-
## Publish a message to a group
209
+
###Publish a message to a group
134
210
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.
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:
152
262
153
263
# [JavaScript](#tab/javascript)
154
264
@@ -165,11 +275,3 @@ await client.SendToGroupAsync("group1", BinaryData.FromString("Hello World"), We
165
275
[.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