|
| 1 | +--- |
| 2 | +title: Azure Web PubSub local tunnel tool (Preview) |
| 3 | +titleSuffix: Azure Web PubSub Service |
| 4 | +description: This article describes the awps-tunnel local tunnel tool to help improve local development experience. |
| 5 | +author: vicancy |
| 6 | +ms.author: lianwei |
| 7 | +ms.service: azure-web-pubsub |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 12/12/2023 |
| 10 | +--- |
| 11 | + |
| 12 | +# Azure Web PubSub local tunnel tool |
| 13 | + |
| 14 | +Web PubSub local tunnel provides a local development environment for customers to enhance their local development experience. There's no need to use third-party tools to expose local ports anymore, use Web PubSub local tunnel as the tunnel between the Web PubSub service and your local server to keep your local development environment secure and safe. |
| 15 | + |
| 16 | +Web PubSub local tunnel provides: |
| 17 | +* A way to tunnel traffic from Web PubSub to your local server |
| 18 | +* A way to view the end-to-end data flow from your client to Web PubSub through the tunnel and to your local server |
| 19 | +* Provides an embedded upstream server for you to get started |
| 20 | +* Provides a simple client for you to get started with your server development |
| 21 | + |
| 22 | +Benefits: |
| 23 | +* Secured local: no need to expose your local server to public |
| 24 | +* Secured connection: use Microsoft Entra ID and Web PubSub access policy to connect |
| 25 | +* Simple configuration: URL template sets to `tunnel:///<your_server_path>` |
| 26 | +* Data inspection: vivid view of the data and the workflow |
| 27 | + |
| 28 | +## Prerequisite |
| 29 | +* [Node.js](https://nodejs.org/) version 16 or higher |
| 30 | + |
| 31 | +## Install |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install -g @azure/web-pubsub-tunnel-tool |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +``` |
| 40 | +Usage: awps-tunnel [options] [command] |
| 41 | +
|
| 42 | +A local tool to help tunnel Azure Web PubSub traffic to local web app and provide a vivid view to the end to end workflow. |
| 43 | +
|
| 44 | +Options: |
| 45 | + -v, --version Show the version number. |
| 46 | + -h, --help Show help details. |
| 47 | +
|
| 48 | +Commands: |
| 49 | + status Show the current configuration status. |
| 50 | + bind [options] Bind configurations to the tool so that you don't need to specify them every time running the tool. |
| 51 | + run [options] Run the tool. |
| 52 | + help [command] Display help details for subcommand. |
| 53 | +
|
| 54 | +You could also set WebPubSubConnectionString environment variable if you don't want to configure endpoint. |
| 55 | +
|
| 56 | +``` |
| 57 | + |
| 58 | +## Prepare the credential |
| 59 | + |
| 60 | +Both connection string and Microsoft Entra ID are supported. |
| 61 | + |
| 62 | +### Using connection string |
| 63 | + |
| 64 | +1. In your Web PubSub service portal, copy your connection string from your Web PubSub service portal. |
| 65 | + |
| 66 | +1. Set the connection string to your local environment variable and start `awps-tunnel`. |
| 67 | + |
| 68 | +# [Linux or macOS](#tab/bash) |
| 69 | +```bash |
| 70 | +export WebPubSubConnectionString="<your connection string>" |
| 71 | +``` |
| 72 | +# [Windows](#tab/cmd) |
| 73 | +```cmd |
| 74 | +SET WebPubSubConnectionString=<your connection string> |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### Using Azure Identity |
| 80 | + |
| 81 | +1. In your Web PubSub service portal, go to Access control tab, and add role `Web PubSub Service Owner` to your identity. |
| 82 | + |
| 83 | +1. In your local terminal, use [Azure CLI](/cli/azure/authenticate-azure-cli) `az login` to sign in to your identity. |
| 84 | + |
| 85 | +1. Alternatively, you could set account information via [defined environment variables](/javascript/api/overview/azure/identity-readme#environment-variables), or use [Managed identity authentication](/entra/identity/managed-identities-azure-resources/overview#which-operations-can-i-perform-on-managed-identities) directly for supported Azure services. |
| 86 | + |
| 87 | +## Run |
| 88 | +1. In your Web PubSub service portal, go to Settings tab, specify the event handler URL template to start with `tunnel:///` to allow tunnel connection. |
| 89 | + |
| 90 | + :::image type="content" alt-text="Screenshot of setting the upstream URL in hub settings." source="media\howto-web-pubsub-tunnel-tool\hub-settings.png" ::: |
| 91 | + |
| 92 | +1. Run the tool with the hub you set before, for example, connect to an endpoint `https://<awps-host-name>.webpubsub.azure.com` with hub `chat`: |
| 93 | + ```bash |
| 94 | + awps-tunnel run --hub chat --endpoint https://<awps-host-name>.webpubsub.azure.com |
| 95 | + ``` |
| 96 | + |
| 97 | + You could also use `awps-tunnel bind --hub chat --endpoint https://<awps-host-name>.webpubsub.azure.com` to save the configuration and then `awps-tunnel run`. |
| 98 | + |
| 99 | +1. You see output like `Open webview at: http://127.0.0.1:4000`, open the link in your browser and you could see the tunnel status and the workflow. |
| 100 | + |
| 101 | +1. Now switch to **Server** tab, and check *Built-in Echo Server* to start a builtin upstream server with code similar to the sample code shown below it. |
| 102 | + |
| 103 | + :::image type="content" alt-text="Screenshot of starting built-in echo server." source="media\howto-web-pubsub-tunnel-tool\overview-upstream.png" ::: |
| 104 | + |
| 105 | +1. Alternatively, you can start your own upstream server at http://localhost:3000. You can also specify option`--upstream http://localhost:<custom-port>` when `awps-tunnel run` or `awps-tunnel bind` to configure your own upstream server at a custom port. For example, run the below code to start this sample [upstream server](https://github.com/Azure/azure-webpubsub/tree/main/tools/awps-tunnel/server/samples/upstream/), when it starts, the upstream serves requests to http://localhost:3000/eventhandler/. |
| 106 | + |
| 107 | + ```bash |
| 108 | + git clone https://github.com/Azure/azure-webpubsub.git |
| 109 | + cd tools/awps-tunnel/server/samples/upstream |
| 110 | + npm install |
| 111 | + npm start |
| 112 | + ``` |
| 113 | + |
| 114 | +1. Now switch to **Client** tab, select `Connect` to start a test WebSocket connection to the Azure Web PubSub service. You would see the traffic goes through Web PubSub to Local Tunnel and finally reaches the upstream server. The tunnel tab provides the details of the request and responses, providing you with a vivid view of what is requesting your upstream server and what is reponding from the upstream server. |
| 115 | + |
| 116 | + :::image type="content" alt-text="Screenshot of starting the test WebSocket connection and send message." source="media\howto-web-pubsub-tunnel-tool\overview-client.png" ::: |
| 117 | + |
| 118 | + :::image type="content" alt-text="Screenshot of showing the traffic inspection." source="media\howto-web-pubsub-tunnel-tool\overview-tunnel.png" ::: |
0 commit comments