|
| 1 | +--- |
| 2 | +title: 'Visual Studio Code Extension for Azure Web PubSub' |
| 3 | +description: Develop with Visual Studio Code extension |
| 4 | +author: xingsy97 |
| 5 | +ms.author: siyuanxing |
| 6 | +ms.service: azure-web-pubsub |
| 7 | +ms.topic: reference |
| 8 | +ms.date: 04/28/2024 |
| 9 | +--- |
| 10 | + |
| 11 | +# Quickstart: Develop with Visual Studio Code Extension |
| 12 | +Azure Web PubSub helps developer build real-time messaging web applications using WebSockets and the publish-subscribe pattern easily. |
| 13 | + |
| 14 | +In this tutorial, you create a chat application using Azure Web PubSub with the help of Visual Studio Code. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- An Azure account with an active subscription is required. If you don't already have one, you can [create an account for free](https://azure.microsoft.com/free). |
| 19 | +- Visual Studio Code, available as a [free download](https://code.visualstudio.com/). |
| 20 | +- The following Visual Studio Code extensions installed: |
| 21 | + - The [Azure Account extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.azure-account) |
| 22 | + - The [Azure Web PubSub extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurewebpubsub) |
| 23 | + |
| 24 | +## Clone the project |
| 25 | + |
| 26 | +1. Open a new Visual Studio Code window. |
| 27 | + |
| 28 | +1. Press <kbd>F1</kbd> to open the command palette. |
| 29 | + |
| 30 | +1. Enter **Git: Clone** and press enter. |
| 31 | + |
| 32 | +1. Enter the following URL to clone the sample project: |
| 33 | + |
| 34 | + ```git |
| 35 | + https://github.com/Azure/azure-webpubsub.git |
| 36 | + ``` |
| 37 | +
|
| 38 | + > [!NOTE] |
| 39 | + > This tutorial uses a JavaScript project, but the steps are language agnostic. |
| 40 | +
|
| 41 | +1. Select a folder to clone the project into. |
| 42 | +
|
| 43 | +1. Select **Open -> Open Folder** to `azure-webpubsub/samples/javascript/chatapp/nativeapi` in Visual Studio Code. |
| 44 | +
|
| 45 | +## Sign in to Azure |
| 46 | +
|
| 47 | +1. Press <kbd>F1</kbd> to open the command palette. |
| 48 | +
|
| 49 | +1. Select **Azure: Sign In** and follow the prompts to authenticate. |
| 50 | +
|
| 51 | +1. Once signed in, return to Visual Studio Code. |
| 52 | +
|
| 53 | +## Create an Azure Web PubSub Service |
| 54 | +
|
| 55 | +The Azure Web PubSub extension for Visual Studio Code enables users to quickly create, manage, and utilize Azure Web PubSub Service and its developer tools such as [Azure Web PubSub Local Tunnel Tool](https://www.npmjs.com/package/@azure/web-pubsub-tunnel-tool). |
| 56 | +In this scenario, you create a new Azure Web PubSub Service resource and configure it to host your application. After installing the Web PubSub extension, you can access its features under the Azure control panel in Visual Studio Code. |
| 57 | +
|
| 58 | +1. Press <kbd>F1</kbd> to open the command palette and run the **Azure Web PubSub: Create Web PubSub Service** command. |
| 59 | +
|
| 60 | +1. Enter the following values as prompted by the extension. |
| 61 | +
|
| 62 | + | Prompt | Value | |
| 63 | + |--|--| |
| 64 | + | Select subscription | Select the Azure subscription you want to use. | |
| 65 | + | Select resource group | Select the Azure resource group you want to use. | |
| 66 | + | Enter a name for the service | Enter `my-wps`. | |
| 67 | + | Select a location | Select an Azure region close to you. | |
| 68 | + | Select a pricing tier | Select a pricing tier you want to use. | |
| 69 | + | Select a unit count | Select a unit count you want to use.| |
| 70 | +
|
| 71 | + The Azure activity log panel opens and displays the deployment progress. This process might take a few minutes to complete. |
| 72 | +
|
| 73 | +1. Once this process finishes, Visual Studio Code displays a notification. |
| 74 | +
|
| 75 | +## Create a hub setting |
| 76 | +1. Open **Azure** icon in the Activity Bar in the left side of Visual Studio Code. |
| 77 | +
|
| 78 | + > [!NOTE] |
| 79 | + > If your activity bar is hidden, you won't be able to access the extension. Show the Activity Bar by clicking **View > Appearance > Show Activity Bar** |
| 80 | +
|
| 81 | +1. In the resource tree, find the Azure Web PubSub resource `my-wps` you created and click it to expand |
| 82 | +
|
| 83 | +1. Right click the item **Hub Settings** and then select **Create Hub Setting** |
| 84 | +
|
| 85 | +1. Input `sample_chat` as the hub name and create the hub setting. It doesn't matter whether to create extra event handlers or not. Wait for the progress notification shown as finished |
| 86 | +
|
| 87 | +1. Below the item **Hub Settings**, a new subitem *Hub sample_chat* shall appear. Right click on the new item and then choose "Attach Local Tunnel" |
| 88 | +
|
| 89 | +1. A notification reminds you to create a tunnel-enabled event handler pops up. Click **Yes** button. Then enter the following values as prompted by the extension |
| 90 | +
|
| 91 | + | Prompt | Value | |
| 92 | + |--|--| |
| 93 | + | Select User Events | Select **All** | |
| 94 | + | Select System Events | Select **connected** | |
| 95 | + | Input Server Port | Enter **8080** | |
| 96 | +
|
| 97 | +1. The extension creates a new terminal to run the Local Tunnel Tool and a notification reminds you to open Local Tunnel Portal shows up. Click the button "Yes" or open "http://localhost:4000" in web browser manually to view the portal. |
| 98 | +
|
| 99 | +## Run the server application |
| 100 | +1. Ensure working directory is `azure-webpubsub/samples/javascript/chatapp/nativeapi` |
| 101 | +
|
| 102 | +1. Install Node.js dependencies |
| 103 | +
|
| 104 | + ```bash |
| 105 | + npm install |
| 106 | + ``` |
| 107 | +
|
| 108 | +1. Open **Azure** icon in the Activity Bar and find the Azure Web PubSub resource `my-wps`. Then right click on the resource item and select **Copy Connection String**. The connection string is copied to your clipboard |
| 109 | +
|
| 110 | +1. Run the server application with copied connection string |
| 111 | +
|
| 112 | + ```bash |
| 113 | + node server.js "<connection-string>" |
| 114 | + ``` |
| 115 | +
|
| 116 | +1. Open `http://localhost:8080/index.html` in browser to try your chat application. |
| 117 | +
|
| 118 | +> [!TIP] |
| 119 | +> Having issues? Let us know on GitHub by opening an issue in the [Azure Web PubSub repo](https://github.com/azure/azure-webpubsub/). |
0 commit comments