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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,69 @@
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
+ ### Breaking Changes
20
+
21
+ - Move `allowedEndpoints` settings into `WebPubSubEventHandlerOptions`. If not set, the default behavior is allowing all the incoming endpoints.
22
+
23
+ ```js
24
+ const handler = new WebPubSubEventHandler("chat", {
25
+ handleConnect(req, res) {
26
+ // You can set the state for the connection, it lasts throughout the lifetime of the connection
27
+ res.setState("calledTime", 1);
28
+ res.success();
29
+ },
30
+ handleUserEvent(req, res) {
31
+ var calledTime = req.context.states.calledTime++;
32
+ console.log(calledTime);
33
+ // You can also set the state here
34
+ res.setState("calledTime", calledTime);
35
+ res.success();
36
+ },
37
+ allowedEndpoints: ["https://xxx.webpubsub.azure.com"]
38
+ });
39
+ ```
40
+
41
+ - Remove `dumpRequest` flag and leverage @azure/logger instead.
42
+
43
+ ## 1.0.0-beta.3 (2021-07-30)
44
+
45
+ - Support reading and setting connection states, sample usage:
46
+ ```js
47
+ const handler = new WebPubSubEventHandler("chat", ["https://xxx.webpubsub.azure.com"], {
48
+ handleConnect(req, res) {
49
+ // You can set the state for the connection, it lasts throughout the lifetime of the connection
50
+ res.setState("calledTime", 1);
51
+ res.success();
52
+ },
53
+ handleUserEvent(req, res) {
54
+ var calledTime = req.context.states.calledTime++;
55
+ console.log(calledTime);
56
+ // You can also set the state here
57
+ res.setState("calledTime", calledTime);
58
+ res.success();
59
+ }
60
+ });
61
+ ```
62
+
63
+ ## 1.0.0-beta.2 (2021-07-20)
64
+
65
+ - Removed unnecessary dependencies.
66
+
1
67
  ## 1.0.0-beta.1 (2021-04-23)
2
68
 
3
69
  This is the first release of the @azure/web-pubsub-express package.
package/README.md CHANGED
@@ -1,16 +1,14 @@
1
1
  # Azure Web PubSub CloudEvents handlers for Express
2
2
 
3
- [Azure Web PubSub Service](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
+ When a WebSocket connection connects, the Web PubSub service transforms the connection lifecycle and messages into [events in CloudEvents format](https://docs.microsoft.com/azure/azure-web-pubsub/concept-service-internals#workflow). This library provides an express middleware to handle events representing the WebSocket connection's lifecycle and messages, as shown in below diagram:
6
6
 
7
- Use the express library to:
7
+ ![cloudevents](https://user-images.githubusercontent.com/668244/140321213-6442b3b8-72ee-4c28-aec1-127f9ea8f5d9.png)
8
8
 
9
- - Add Web PubSub CloudEvents middleware to handle incoming client events
10
- - Handle abuse validation requests
11
- - Handle client events requests
9
+ Details about the terms used here are described in [Key concepts](#key-concepts) section.
12
10
 
13
- [Source code](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub-express) |
11
+ [Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub-express) |
14
12
  [Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub-express) |
15
13
  [API reference documentation](https://aka.ms/awps/sdk/js) |
16
14
  [Product documentation](https://aka.ms/awps/doc) |
@@ -20,7 +18,7 @@ Use the express library to:
20
18
 
21
19
  ### Currently supported environments
22
20
 
23
- - [Node.js](https://nodejs.org/) version 8.x.x or higher
21
+ - [LTS versions of Node.js](https://nodejs.org/about/releases/)
24
22
  - [Express](https://expressjs.com/) version 4.x.x or higher
25
23
 
26
24
  ### Prerequisites
@@ -34,24 +32,13 @@ Use the express library to:
34
32
  npm install @azure/web-pubsub-express
35
33
  ```
36
34
 
37
- ### 2. Create a WebPubSubEventHandler
35
+ ### 2. Create a `WebPubSubEventHandler`
38
36
 
39
37
  ```js
40
38
  const express = require("express");
41
39
 
42
40
  const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
43
- const handler = new WebPubSubEventHandler(
44
- "chat",
45
- ["https://<yourAllowedService>.webpubsub.azure.com"],
46
- {
47
- handleConnect: (req, res) => {
48
- // auth the connection and set the userId of the connection
49
- res.success({
50
- userId: "<userId>"
51
- });
52
- }
53
- }
54
- );
41
+ const handler = new WebPubSubEventHandler("chat");
55
42
 
56
43
  const app = express();
57
44
 
@@ -64,17 +51,17 @@ app.listen(3000, () =>
64
51
 
65
52
  ## Key concepts
66
53
 
67
- ### Hub
54
+ ### Connection
68
55
 
69
- Hub is a logic 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.
56
+ 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.
70
57
 
71
- ### Group
58
+ ### Hub
72
59
 
73
- 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.
60
+ 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.
74
61
 
75
62
  ### Group
76
63
 
77
- 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.
64
+ 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.
78
65
 
79
66
  ### User
80
67
 
@@ -90,11 +77,117 @@ Event handler contains the logic to handle the client events. Event handler need
90
77
 
91
78
  ## Examples
92
79
 
80
+ ### Handle the `connect` request and assign `<userId>`
81
+
82
+ ```js
83
+ const express = require("express");
84
+
85
+ const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
86
+ const handler = new WebPubSubEventHandler("chat", {
87
+ handleConnect: (req, res) => {
88
+ // auth the connection and set the userId of the connection
89
+ res.success({
90
+ userId: "<userId>"
91
+ });
92
+ },
93
+ allowedEndpoints: ["https://<yourAllowedService>.webpubsub.azure.com"]
94
+ });
95
+
96
+ const app = express();
97
+
98
+ app.use(handler.getMiddleware());
99
+
100
+ app.listen(3000, () =>
101
+ console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`)
102
+ );
103
+ ```
104
+
105
+ ### Only allow specified endpoints
106
+
107
+ ```js
108
+ const express = require("express");
109
+
110
+ const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
111
+ const handler = new WebPubSubEventHandler("chat", {
112
+ allowedEndpoints: [
113
+ "https://<yourAllowedService1>.webpubsub.azure.com",
114
+ "https://<yourAllowedService2>.webpubsub.azure.com"
115
+ ]
116
+ });
117
+
118
+ const app = express();
119
+
120
+ app.use(handler.getMiddleware());
121
+
122
+ app.listen(3000, () =>
123
+ console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`)
124
+ );
125
+ ```
126
+
127
+ ### Set custom event handler path
128
+
129
+ ```js
130
+ const express = require("express");
131
+
132
+ const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
133
+ const handler = new WebPubSubEventHandler("chat", {
134
+ path: "customPath1"
135
+ });
136
+
137
+ const app = express();
138
+
139
+ app.use(handler.getMiddleware());
140
+
141
+ app.listen(3000, () =>
142
+ // Azure WebPubSub Upstream ready at http://localhost:3000/customPath1
143
+ console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`)
144
+ );
145
+ ```
146
+
147
+ ### Set and read connection state
148
+
149
+ ```js
150
+ const express = require("express");
151
+
152
+ const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
153
+
154
+ const handler = new WebPubSubEventHandler("chat", {
155
+ handleConnect(req, res) {
156
+ // You can set the state for the connection, it lasts throughout the lifetime of the connection
157
+ res.setState("calledTime", 1);
158
+ res.success();
159
+ },
160
+ handleUserEvent(req, res) {
161
+ var calledTime = req.context.states.calledTime++;
162
+ console.log(calledTime);
163
+ // You can also set the state here
164
+ res.setState("calledTime", calledTime);
165
+ res.success();
166
+ }
167
+ });
168
+
169
+ const app = express();
170
+
171
+ app.use(handler.getMiddleware());
172
+
173
+ app.listen(3000, () =>
174
+ console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`)
175
+ );
176
+ ```
177
+
93
178
  ## Troubleshooting
94
179
 
95
- ### Dump request
180
+ ### Enable logs
181
+
182
+ You can set the following environment variable to get the debug logs when using this library.
183
+
184
+ - Getting debug logs from the SignalR client library
185
+
186
+ ```bash
187
+ export AZURE_LOG_LEVEL=verbose
188
+ ```
96
189
 
97
- Set `dumpRequest` to `true` to view the incoming requests.
190
+ 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).
98
191
 
99
192
  ### Live Trace
100
193
 
@@ -108,11 +201,11 @@ directory for detailed examples on how to use this library.
108
201
 
109
202
  ## Contributing
110
203
 
111
- 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.
204
+ 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.
112
205
 
113
206
  ## Related projects
114
207
 
115
208
  - [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)
116
209
 
117
210
  [azure_sub]: https://azure.microsoft.com/free/
118
- [samples_ref]: https://github.com/Azure/azure-webpubsub/tree/main/samples
211
+ [samples_ref]: https://github.com/Azure/azure-webpubsub/tree/main/samples/javascript/