diff --git a/sdk/web-pubsub/web-pubsub-express/README.md b/sdk/web-pubsub/web-pubsub-express/README.md index 9d131de10a4b..63e63e6cb1b3 100644 --- a/sdk/web-pubsub/web-pubsub-express/README.md +++ b/sdk/web-pubsub/web-pubsub-express/README.md @@ -1,21 +1,18 @@ # Azure Web PubSub CloudEvents handlers for Express -[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. +[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. -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. +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: -Use the express library to: +![cloudevents](https://user-images.githubusercontent.com/668244/140321213-6442b3b8-72ee-4c28-aec1-127f9ea8f5d9.png) -- Add Web PubSub CloudEvents middleware to handle incoming client events - - Handle abuse validation requests - - Handle client events requests - -Key links: -- [Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub-express) -- [Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub-express) -- [API reference documentation](https://aka.ms/awps/sdk/js) -- [Product documentation](https://aka.ms/awps/doc) -- [Samples][samples_ref] +Details about the terms used here are described in [Key concepts](#key-concepts) section. + +[Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub-express) | +[Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub-express) | +[API reference documentation](https://aka.ms/awps/sdk/js) | +[Product documentation](https://aka.ms/awps/doc) | +[Samples][samples_ref] ## Getting started @@ -35,24 +32,18 @@ Key links: npm install @azure/web-pubsub-express ``` -### 2. Create a WebPubSubEventHandler +### 2. Create a `WebPubSubEventHandler` ```js const express = require("express"); const { WebPubSubEventHandler } = require("@azure/web-pubsub-express"); -const handler = new WebPubSubEventHandler( - "chat", - { - handleConnect: (req, res) => { - // auth the connection and set the userId of the connection - res.success({ - userId: "" - }); - }, - allowedEndpoints: ["https://.webpubsub.azure.com"] +const handler = new WebPubSubEventHandler("chat", { + handleConnect: (req, res) => { + // auth the connection and set the userId of the connection + res.success(); } -); +}); const app = express(); @@ -65,13 +56,17 @@ app.listen(3000, () => ## Key concepts +### Connection + +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. + ### Hub -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. +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. ### Group -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. +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. ### User @@ -87,11 +82,123 @@ Event handler contains the logic to handle the client events. Event handler need ## Examples +### Handle the `connect` request and assign `` + +```js +const express = require("express"); + +const { WebPubSubEventHandler } = require("@azure/web-pubsub-express"); +const handler = new WebPubSubEventHandler("chat", { + handleConnect: (req, res) => { + // auth the connection and set the userId of the connection + res.success({ + userId: "" + }); + }, + allowedEndpoints: ["https://.webpubsub.azure.com"] +}); + +const app = express(); + +app.use(handler.getMiddleware()); + +app.listen(3000, () => + console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`) +); +``` + +### Only allow specified endpoints + +```js +const express = require("express"); + +const { WebPubSubEventHandler } = require("@azure/web-pubsub-express"); +const handler = new WebPubSubEventHandler("chat", { + handleConnect: (req, res) => { + res.success(); + }, + allowedEndpoints: [ + "https://.webpubsub.azure.com", + "https://.webpubsub.azure.com" + ] +}); + +const app = express(); + +app.use(handler.getMiddleware()); + +app.listen(3000, () => + console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`) +); +``` + +### Set custom event handler path + +```js +const express = require("express"); + +const { WebPubSubEventHandler } = require("@azure/web-pubsub-express"); +const handler = new WebPubSubEventHandler("chat", { + path: "customPath1", + handleConnect: (req, res) => { + // auth the connection and set the userId of the connection + res.success(); + } +}); + +const app = express(); + +app.use(handler.getMiddleware()); + +app.listen(3000, () => + console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`) +); +``` + +### Set and read connection state + +```js +const express = require("express"); + +const { WebPubSubEventHandler } = require("@azure/web-pubsub-express"); + +const handler = new WebPubSubEventHandler("chat", { + handleConnect(req, res) { + // You can set the state for the connection, it lasts throughout the lifetime of the connection + res.setState("calledTime", 1); + res.success(); + }, + handleUserEvent(req, res) { + var calledTime = req.context.states.calledTime++; + console.log(calledTime); + // You can also set the state here + res.setState("calledTime", calledTime); + res.success(); + } +}); + +const app = express(); + +app.use(handler.getMiddleware()); + +app.listen(3000, () => + console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`) +); +``` + ## Troubleshooting -### Dump request +### Enable logs + +You can set the following environment variable to get the debug logs when using this library. + +- Getting debug logs from the SignalR client library + +```bash +export AZURE_LOG_LEVEL=verbose +``` -Set `dumpRequest` to `true` to view the incoming requests. +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). ### Live Trace @@ -112,4 +219,4 @@ If you'd like to contribute to this library, please read the [contributing guide - [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js) [azure_sub]: https://azure.microsoft.com/free/ -[samples_ref]: https://github.com/Azure/azure-webpubsub/tree/main/samples +[samples_ref]: https://github.com/Azure/azure-webpubsub/tree/main/samples/javascript/ diff --git a/sdk/web-pubsub/web-pubsub/README.md b/sdk/web-pubsub/web-pubsub/README.md index e2d787e2b247..d2ce147ed822 100644 --- a/sdk/web-pubsub/web-pubsub/README.md +++ b/sdk/web-pubsub/web-pubsub/README.md @@ -2,7 +2,9 @@ [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. -This library can be used to do the following actions. Details about the terms used here are described in [Key concepts](#key-concepts) section. +You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram: + +![overflow](https://user-images.githubusercontent.com/668244/140014067-25a00959-04dc-47e8-ac25-6957bd0a71ce.png). - Send messages to hubs and groups. - Send messages to particular users and connections. @@ -10,11 +12,13 @@ This library can be used to do the following actions. Details about the terms us - Close connections - Grant, revoke, and check permissions for an existing connection -[Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub) | -[Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub) | +Details about the terms used here are described in [Key concepts](#key-concepts) section. + +[Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub) | +[Package (NPM)](https://www.npmjs.com/package/@azure/web-pubsub) | [API reference documentation](https://aka.ms/awps/sdk/js) | [Product documentation](https://aka.ms/awps/doc) | -[Samples][samples_ref] +[Samples][samples_ref] ## Getting started @@ -209,6 +213,10 @@ export AZURE_LOG_LEVEL=verbose 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). +### Live Trace + +Use **Live Trace** from the Web PubSub service portal to view the live traffic. + ## Next steps Please take a look at the diff --git a/sdk/web-pubsub/web-pubsub/{ b/sdk/web-pubsub/web-pubsub/{ deleted file mode 100644 index e69de29bb2d1..000000000000