@azure/web-pubsub 1.0.0-beta.3 → 1.0.1-alpha.20220103.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +107 -33
  3. package/dist/index.js +1048 -233
  4. package/dist/index.js.map +1 -1
  5. package/dist-esm/samples-dev/directMessage.js +1 -1
  6. package/dist-esm/samples-dev/directMessage.js.map +1 -1
  7. package/dist-esm/src/generated/generatedClient.js +3 -3
  8. package/dist-esm/src/generated/generatedClient.js.map +1 -1
  9. package/dist-esm/src/generated/generatedClientContext.js +8 -8
  10. package/dist-esm/src/generated/generatedClientContext.js.map +1 -1
  11. package/dist-esm/src/generated/models/index.js +6 -1
  12. package/dist-esm/src/generated/models/index.js.map +1 -1
  13. package/dist-esm/src/generated/models/mappers.js +368 -0
  14. package/dist-esm/src/generated/models/mappers.js.map +1 -0
  15. package/dist-esm/src/generated/models/parameters.js +105 -23
  16. package/dist-esm/src/generated/models/parameters.js.map +1 -1
  17. package/dist-esm/src/generated/operations/healthApi.js +4 -3
  18. package/dist-esm/src/generated/operations/healthApi.js.map +1 -1
  19. package/dist-esm/src/generated/operations/webPubSub.js +315 -59
  20. package/dist-esm/src/generated/operations/webPubSub.js.map +1 -1
  21. package/dist-esm/src/generated/operationsInterfaces/webPubSub.js.map +1 -1
  22. package/dist-esm/src/groupClient.js +32 -21
  23. package/dist-esm/src/groupClient.js.map +1 -1
  24. package/dist-esm/src/hubClient.js +162 -107
  25. package/dist-esm/src/hubClient.js.map +1 -1
  26. package/dist-esm/src/index.js +1 -1
  27. package/dist-esm/src/index.js.map +1 -1
  28. package/dist-esm/src/logger.js +1 -1
  29. package/dist-esm/src/logger.js.map +1 -1
  30. package/dist-esm/src/parseConnectionString.js +3 -1
  31. package/dist-esm/src/parseConnectionString.js.map +1 -1
  32. package/dist-esm/src/reverseProxyPolicy.js +21 -0
  33. package/dist-esm/src/reverseProxyPolicy.js.map +1 -0
  34. package/dist-esm/src/tracing.js +1 -1
  35. package/dist-esm/src/tracing.js.map +1 -1
  36. package/dist-esm/src/utils.js +2 -0
  37. package/dist-esm/src/utils.js.map +1 -1
  38. package/dist-esm/src/webPubSubCredentialPolicy.js +2 -2
  39. package/dist-esm/src/webPubSubCredentialPolicy.js.map +1 -1
  40. package/dist-esm/test/conn.spec.js +2 -2
  41. package/dist-esm/test/conn.spec.js.map +1 -1
  42. package/dist-esm/test/groups.spec.js +14 -6
  43. package/dist-esm/test/groups.spec.js.map +1 -1
  44. package/dist-esm/test/hubs.spec.js +53 -10
  45. package/dist-esm/test/hubs.spec.js.map +1 -1
  46. package/dist-esm/test/integration.spec.js +128 -0
  47. package/dist-esm/test/integration.spec.js.map +1 -0
  48. package/dist-esm/test/testEnv.js +6 -2
  49. package/dist-esm/test/testEnv.js.map +1 -1
  50. package/package.json +32 -32
  51. package/types/web-pubsub.d.ts +165 -109
  52. package/dist-esm/src/normalizeOptions.js +0 -8
  53. package/dist-esm/src/normalizeOptions.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # Release History
2
2
 
3
+ ## 1.0.1 (Unreleased)
4
+
5
+ ### Features Added
6
+
7
+ ### Breaking Changes
8
+
9
+ ### Bugs Fixed
10
+
11
+ ### Other Changes
12
+
13
+ ## 1.0.0 (2021-11-11)
14
+
15
+ No changes.
16
+
17
+ ## 1.0.0-beta.4 (2021-11-09)
18
+
19
+ ### Features Added
20
+
21
+ - Support for DefaultAzureCredential
22
+ - Added support for reverse proxies, enabling usage of Web Pubsub with Azure API Management.
23
+ - Added method to close all connections in a hub `serviceClient.closeAllConnections`,
24
+ - Added method to close all connections in a group `groupClient.closeAllConnections`
25
+ - Added method to close all connections for a user `serviceClient.closeUserConnections`.
26
+
27
+ ### Breaking Changes
28
+
29
+ - Renamed method `getAuthenticationToken` to `getClientAccessToken`
30
+ - Renamed method `hasUser` to `userExists`
31
+ - Renamed method `hasGroup` to `groupExists`
32
+ - Renamed method `hasConnection` to `connnectionExists`
33
+
3
34
  ## 1.0.0-beta.3 (2021-07-07)
4
35
 
5
36
  ### Breaking Changes
package/README.md CHANGED
@@ -1,36 +1,24 @@
1
1
  # Azure Web PubSub service client library for JavaScript
2
2
 
3
- [Azure Web PubSub](https://aka.ms/awps/doc) is a service that enables you to build real-time messaging web applications using WebSockets and the publish-subscribe pattern. Any platform supporting WebSocket APIs can connect to the service easily, e.g. web pages, mobile applications, edge devices, etc. The service manages the WebSocket connections for you and allows up to 100K concurrent connections. It provides powerful APIs for you to manage these clients and deliver real-time messages.
3
+ [Azure Web PubSub service](https://aka.ms/awps/doc) is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests can also use Azure Web PubSub service.
4
4
 
5
- Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients, can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests, can also use Azure Web PubSub service.
5
+ You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:
6
6
 
7
- We list some examples that are good to use Azure Web PubSub service:
8
-
9
- - **High frequency data updates:** gaming, voting, polling, auction.
10
- - **Live dashboards and monitoring:** company dashboard, financial market data, instant sales update, multi-player game leader board, and IoT monitoring.
11
- - **Cross-platform live chat:** live chat room, chat bot, on-line customer support, real-time shopping assistant, messenger, in-game chat, and so on.
12
- - **Real-time location on map:** logistic tracking, delivery status tracking, transportation status updates, GPS apps.
13
- - **Real-time targeted ads:** personalized real-time push ads and offers, interactive ads.
14
- - **Collaborative apps:** coauthoring, whiteboard apps and team meeting software.
15
- - **Push instant notifications:** social network, email, game, travel alert.
16
- - **Real-time broadcasting:** live audio/video broadcasting, live captioning, translating, events/news broadcasting.
17
- - **IoT and connected devices:** real-time IoT metrics, remote control, real-time status, and location tracking.
18
- - **Automation:** real-time trigger from upstream events.
19
-
20
- Use the library to:
7
+ ![overflow](https://user-images.githubusercontent.com/668244/140014067-25a00959-04dc-47e8-ac25-6957bd0a71ce.png).
21
8
 
22
9
  - Send messages to hubs and groups.
23
10
  - Send messages to particular users and connections.
24
11
  - Organize users and connections into groups.
25
12
  - Close connections
26
- - Grant/revoke/check permissions for an existing connection
13
+ - Grant, revoke, and check permissions for an existing connection
27
14
 
28
- Key links:
29
- - [Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub)
30
- - [Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub)
31
- - [API reference documentation](https://aka.ms/awps/sdk/js)
32
- - [Product documentation](https://aka.ms/awps/doc)
33
- - [Samples][samples_ref]
15
+ Details about the terms used here are described in [Key concepts](#key-concepts) section.
16
+
17
+ [Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub) |
18
+ [Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub) |
19
+ [API reference documentation](https://aka.ms/awps/sdk/js) |
20
+ [Product documentation](https://aka.ms/awps/doc) |
21
+ [Samples][samples_ref]
34
22
 
35
23
  ## Getting started
36
24
 
@@ -66,19 +54,37 @@ const key = new AzureKeyCredential("<Key>");
66
54
  const serviceClient = new WebPubSubServiceClient("<Endpoint>", key, "<hubName>");
67
55
  ```
68
56
 
57
+ Or authenticate the `WebPubSubServiceClient` using [Azure Active Directory][aad_doc]
58
+
59
+ 1. Install the `@azure/identity` dependency
60
+
61
+ ```bash
62
+ npm install @azure/identity
63
+ ```
64
+
65
+ 2. Update the source code to use `DefaultAzureCredential`:
66
+
67
+ ```js
68
+ const { WebPubSubServiceClient, AzureKeyCredential } = require("@azure/web-pubsub");
69
+ const { DefaultAzureCredential } = require("@azure/identity");
70
+
71
+ const key = new DefaultAzureCredential();
72
+ const serviceClient = new WebPubSubServiceClient("<Endpoint>", key, "<hubName>");
73
+ ```
74
+
69
75
  ## Key concepts
70
76
 
71
77
  ### Connection
72
78
 
73
- Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.
79
+ A connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.
74
80
 
75
81
  ### Hub
76
82
 
77
- Hub is a logical set of connections. All connections to Web PubSub connect to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. For example, hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names.
83
+ A hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.
78
84
 
79
85
  ### Group
80
86
 
81
- Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients.
87
+ A group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. A client can join multiple groups, and a group can contain multiple clients.
82
88
 
83
89
  ### User
84
90
 
@@ -86,37 +92,100 @@ Connections to Web PubSub can belong to one user. A user might have multiple con
86
92
 
87
93
  ### Message
88
94
 
89
- A message is either a UTF-8 encoded string or raw binary data.
95
+ When the client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.
90
96
 
91
97
  ## Examples
92
98
 
93
- ### Broadcast a JSON message to all users
99
+ ### Get the access token for a client to start the WebSocket connection
94
100
 
95
101
  ```js
96
102
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
97
103
 
98
104
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
99
- await serviceClient.sendToAll({ message: "Hello world!" });
105
+
106
+ // Get the access token for the WebSocket client connection to use
107
+ let token = await serviceClient.getClientAccessToken();
108
+
109
+ // Or get the access token and assign the client a userId
110
+ token = await serviceClient.getClientAccessToken({ userId: "user1" });
111
+
112
+ // return the token to the WebSocket client
100
113
  ```
101
114
 
102
- ### Broadcast a plain text message to all users
115
+ ### Broadcast messages to all connections in a hub
103
116
 
104
117
  ```js
105
118
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
106
119
 
107
120
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
121
+
122
+ // Send a JSON message
123
+ await serviceClient.sendToAll({ message: "Hello world!" });
124
+
125
+ // Send a plain text message
108
126
  await serviceClient.sendToAll("Hi there!", { contentType: "text/plain" });
127
+
128
+ // Send a binary message
129
+ const payload = new Uint8Array(10);
130
+ await serviceClient.sendToAll(payload.buffer);
109
131
  ```
110
132
 
111
- ### Broadcast a binary message to all users
133
+ ### Send messages to all connections in a group
112
134
 
113
135
  ```js
114
136
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
115
137
 
116
138
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
117
139
 
140
+ const groupClient = serviceClient.group("<groupName>");
141
+
142
+ // Add user to the group
143
+ await groupClient.addUser("user1");
144
+
145
+ // Send a JSON message
146
+ await groupClient.sendToAll({ message: "Hello world!" });
147
+
148
+ // Send a plain text message
149
+ await groupClient.sendToAll("Hi there!", { contentType: "text/plain" });
150
+
151
+ // Send a binary message
118
152
  const payload = new Uint8Array(10);
119
- await serviceClient.sendToAll(payload.buffer);
153
+ await groupClient.sendToAll(payload.buffer);
154
+ ```
155
+
156
+ ### Send messages to all connections for a user
157
+
158
+ ```js
159
+ const { WebPubSubServiceClient } = require("@azure/web-pubsub");
160
+
161
+ const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
162
+
163
+ // Send a JSON message
164
+ await serviceClient.sendToUser("user1", { message: "Hello world!" });
165
+
166
+ // Send a plain text message
167
+ await serviceClient.sendToUser("user1", "Hi there!", { contentType: "text/plain" });
168
+
169
+ // Send a binary message
170
+ const payload = new Uint8Array(10);
171
+ await serviceClient.sendToUser("user1", payload.buffer);
172
+ ```
173
+
174
+ ### Check if the group has any connection
175
+
176
+ ```js
177
+ const { WebPubSubServiceClient } = require("@azure/web-pubsub");
178
+ const WebSocket = require("ws");
179
+
180
+ const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
181
+
182
+ const groupClient = serviceClient.group("<groupName>");
183
+
184
+ // close all the connections in the group
185
+ await groupClient.closeAllConnections({ reason: "<closeReason>" });
186
+
187
+ // check if the group has any connections
188
+ const hasConnections = await serviceClient.groupExists("<groupName>");
120
189
  ```
121
190
 
122
191
  ### Access the raw HTTP response for an operation
@@ -124,7 +193,7 @@ await serviceClient.sendToAll(payload.buffer);
124
193
  ```js
125
194
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
126
195
 
127
- function onResponse(rawResponse: FullOperationResponse): void {
196
+ function onResponse(rawResponse) {
128
197
  console.log(rawResponse);
129
198
  }
130
199
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
@@ -145,6 +214,10 @@ export AZURE_LOG_LEVEL=verbose
145
214
 
146
215
  For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
147
216
 
217
+ ### Live Trace
218
+
219
+ Use **Live Trace** from the Web PubSub service portal to view the live traffic.
220
+
148
221
  ## Next steps
149
222
 
150
223
  Please take a look at the
@@ -161,3 +234,4 @@ If you'd like to contribute to this library, please read the [contributing guide
161
234
 
162
235
  [azure_sub]: https://azure.microsoft.com/free/
163
236
  [samples_ref]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/web-pubsub/web-pubsub/samples
237
+ [aad_doc]: https://aka.ms/awps/aad