@azure/web-pubsub 1.0.0-beta.2 → 1.0.1-alpha.20211215.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +118 -31
  3. package/dist/index.js +1315 -567
  4. package/dist/index.js.map +1 -1
  5. package/dist-esm/samples-dev/broadcasting.js +16 -16
  6. package/dist-esm/samples-dev/broadcasting.js.map +1 -1
  7. package/dist-esm/samples-dev/directMessage.js +9 -9
  8. package/dist-esm/samples-dev/directMessage.js.map +1 -1
  9. package/dist-esm/samples-dev/managingGroups.js +11 -12
  10. package/dist-esm/samples-dev/managingGroups.js.map +1 -1
  11. package/dist-esm/src/generated/generatedClient.js +23 -0
  12. package/dist-esm/src/generated/generatedClient.js.map +1 -0
  13. package/dist-esm/src/generated/generatedClientContext.js +41 -0
  14. package/dist-esm/src/generated/generatedClientContext.js.map +1 -0
  15. package/dist-esm/src/generated/index.js +10 -2
  16. package/dist-esm/src/generated/index.js.map +1 -1
  17. package/dist-esm/src/generated/models/index.js +13 -1
  18. package/dist-esm/src/generated/models/index.js.map +1 -1
  19. package/dist-esm/src/generated/models/mappers.js +368 -0
  20. package/dist-esm/src/generated/models/mappers.js.map +1 -0
  21. package/dist-esm/src/generated/models/parameters.js +124 -43
  22. package/dist-esm/src/generated/models/parameters.js.map +1 -1
  23. package/dist-esm/src/generated/operations/healthApi.js +16 -11
  24. package/dist-esm/src/generated/operations/healthApi.js.map +1 -1
  25. package/dist-esm/src/generated/operations/index.js +7 -0
  26. package/dist-esm/src/generated/operations/index.js.map +1 -1
  27. package/dist-esm/src/generated/operations/webPubSub.js +341 -175
  28. package/dist-esm/src/generated/operations/webPubSub.js.map +1 -1
  29. package/dist-esm/src/generated/operationsInterfaces/healthApi.js +9 -0
  30. package/dist-esm/src/generated/operationsInterfaces/healthApi.js.map +1 -0
  31. package/dist-esm/src/generated/operationsInterfaces/index.js +10 -0
  32. package/dist-esm/src/generated/operationsInterfaces/index.js.map +1 -0
  33. package/dist-esm/src/generated/operationsInterfaces/webPubSub.js +9 -0
  34. package/dist-esm/src/generated/operationsInterfaces/webPubSub.js.map +1 -0
  35. package/dist-esm/src/groupClient.js +74 -92
  36. package/dist-esm/src/groupClient.js.map +1 -1
  37. package/dist-esm/src/hubClient.js +280 -196
  38. package/dist-esm/src/hubClient.js.map +1 -1
  39. package/dist-esm/src/index.js.map +1 -1
  40. package/dist-esm/src/logger.js +1 -1
  41. package/dist-esm/src/logger.js.map +1 -1
  42. package/dist-esm/src/parseConnectionString.js +3 -1
  43. package/dist-esm/src/parseConnectionString.js.map +1 -1
  44. package/dist-esm/src/reverseProxyPolicy.js +21 -0
  45. package/dist-esm/src/reverseProxyPolicy.js.map +1 -0
  46. package/dist-esm/src/utils.js +8 -6
  47. package/dist-esm/src/utils.js.map +1 -1
  48. package/dist-esm/src/webPubSubCredentialPolicy.js +18 -18
  49. package/dist-esm/src/webPubSubCredentialPolicy.js.map +1 -1
  50. package/dist-esm/test/conn.spec.js +7 -8
  51. package/dist-esm/test/conn.spec.js.map +1 -1
  52. package/dist-esm/test/groups.spec.js +39 -38
  53. package/dist-esm/test/groups.spec.js.map +1 -1
  54. package/dist-esm/test/hubs.spec.js +95 -49
  55. package/dist-esm/test/hubs.spec.js.map +1 -1
  56. package/dist-esm/test/integration.spec.js +128 -0
  57. package/dist-esm/test/integration.spec.js.map +1 -0
  58. package/dist-esm/test/testEnv.js +5 -1
  59. package/dist-esm/test/testEnv.js.map +1 -1
  60. package/package.json +37 -36
  61. package/types/web-pubsub.d.ts +186 -138
  62. package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js +0 -15
  63. package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js.map +0 -1
  64. package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js +0 -34
  65. package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js.map +0 -1
  66. package/dist-esm/src/normalizeOptions.js +0 -8
  67. package/dist-esm/src/normalizeOptions.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Release History
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
+
34
+ ## 1.0.0-beta.3 (2021-07-07)
35
+
36
+ ### Breaking Changes
37
+
38
+ - `hasUser` has been removed from `GroupClient` as that operation is no longer supported by the service.
39
+ - Updated to have void returns for most operations. If you were previously using `RestResponse`, you can instead use the `onResponse` callback in the operation options. See README for an example.
40
+
2
41
  ## 1.0.0-beta.2 (2021-05-19)
42
+
3
43
  Remove "url" dependency
4
44
 
5
45
  ## 1.0.0-beta.1 (2021-04-23)
package/README.md CHANGED
@@ -1,31 +1,20 @@
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
- [Source code](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub) |
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) |
29
18
  [Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub) |
30
19
  [API reference documentation](https://aka.ms/awps/sdk/js) |
31
20
  [Product documentation](https://aka.ms/awps/doc) |
@@ -35,7 +24,7 @@ Use the library to:
35
24
 
36
25
  ### Currently supported environments
37
26
 
38
- - [Node.js](https://nodejs.org/) version 8.x.x or higher
27
+ - [LTS versions of Node.js](https://nodejs.org/about/releases/)
39
28
 
40
29
  ### Prerequisites
41
30
 
@@ -65,19 +54,37 @@ const key = new AzureKeyCredential("<Key>");
65
54
  const serviceClient = new WebPubSubServiceClient("<Endpoint>", key, "<hubName>");
66
55
  ```
67
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
+
68
75
  ## Key concepts
69
76
 
70
77
  ### Connection
71
78
 
72
- 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.
73
80
 
74
81
  ### Hub
75
82
 
76
- 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.
77
84
 
78
85
  ### Group
79
86
 
80
- 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.
81
88
 
82
89
  ### User
83
90
 
@@ -85,37 +92,112 @@ Connections to Web PubSub can belong to one user. A user might have multiple con
85
92
 
86
93
  ### Message
87
94
 
88
- 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.
89
96
 
90
97
  ## Examples
91
98
 
92
- ### Broadcast a JSON message to all users
99
+ ### Get the access token for a client to start the WebSocket connection
93
100
 
94
101
  ```js
95
102
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
96
103
 
97
104
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
98
- 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
99
113
  ```
100
114
 
101
- ### Broadcast a plain text message to all users
115
+ ### Broadcast messages to all connections in a hub
102
116
 
103
117
  ```js
104
118
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
105
119
 
106
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
107
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);
108
131
  ```
109
132
 
110
- ### Broadcast a binary message to all users
133
+ ### Send messages to all connections in a group
111
134
 
112
135
  ```js
113
136
  const { WebPubSubServiceClient } = require("@azure/web-pubsub");
114
137
 
115
138
  const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
116
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
117
152
  const payload = new Uint8Array(10);
118
- 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>");
189
+ ```
190
+
191
+ ### Access the raw HTTP response for an operation
192
+
193
+ ```js
194
+ const { WebPubSubServiceClient } = require("@azure/web-pubsub");
195
+
196
+ function onResponse(rawResponse) {
197
+ console.log(rawResponse);
198
+ }
199
+ const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
200
+ await serviceClient.sendToAll({ message: "Hello world!" }, { onResponse });
119
201
  ```
120
202
 
121
203
  ## Troubleshooting
@@ -130,7 +212,11 @@ You can set the following environment variable to get the debug logs when using
130
212
  export AZURE_LOG_LEVEL=verbose
131
213
  ```
132
214
 
133
- 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/master/sdk/core/logger).
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).
216
+
217
+ ### Live Trace
218
+
219
+ Use **Live Trace** from the Web PubSub service portal to view the live traffic.
134
220
 
135
221
  ## Next steps
136
222
 
@@ -140,11 +226,12 @@ directory for detailed examples on how to use this library.
140
226
 
141
227
  ## Contributing
142
228
 
143
- If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/master/CONTRIBUTING.md) to learn more about how to build and test the code.
229
+ If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.
144
230
 
145
231
  ## Related projects
146
232
 
147
233
  - [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)
148
234
 
149
235
  [azure_sub]: https://azure.microsoft.com/free/
150
- [samples_ref]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/web-pubsub/web-pubsub/samples
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