|
| 1 | +--- |
| 2 | +title: "Create a custom web interface" |
| 3 | +linkTitle: "Create a custom web interface" |
| 4 | +weight: 11 |
| 5 | +no_list: true |
| 6 | +type: docs |
| 7 | +description: "Create and deploy single page applications on the Viam platform." |
| 8 | +--- |
| 9 | + |
| 10 | +With single page apps you can create and deploy custom web interfaces for your machines that use a single HTML page. |
| 11 | +Single page apps are accessible from a dedicated URL (`appname.publicnamespace.viamapps.com`) and hosting and authentication is handled for you. |
| 12 | + |
| 13 | +When opening an app, users log in and then select a machine they have access to. |
| 14 | +Then your app is rendered and ready for use. |
| 15 | + |
| 16 | +TODO: Example GIF |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +{{< expand "Install the Viam CLI and authenticate." >}} |
| 21 | +Install the Viam CLI using the option below that matches your system architecture: |
| 22 | + |
| 23 | +{{< readfile "/static/include/how-to/install-cli.md" >}} |
| 24 | + |
| 25 | +Then authenticate your CLI session with Viam using one of the following options: |
| 26 | + |
| 27 | +{{< readfile "/static/include/how-to/auth-cli.md" >}} |
| 28 | + |
| 29 | +{{< /expand >}} |
| 30 | + |
| 31 | +## Create a single page app |
| 32 | + |
| 33 | +{{< table >}} |
| 34 | +{{% tablestep number=1 %}} |
| 35 | + |
| 36 | +**Build your single page application** using your preferred framework like React, Vue, Angular, or others. |
| 37 | +Your application should be built and ready for deployment, with all assets compiled into a distributable format. |
| 38 | + |
| 39 | +TODO: cover dev process |
| 40 | +TODO: how do you connect to the machine / how do you access the api key? |
| 41 | + |
| 42 | +{{% /tablestep %}} |
| 43 | +{{% tablestep number=2 %}} |
| 44 | + |
| 45 | +**Create a <FILE>meta.json</FILE>** file using this template: |
| 46 | + |
| 47 | +{{< tabs >}} |
| 48 | +{{% tab name="Template" %}} |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "module_id": "your-namespace:your-module", |
| 53 | + "visibility": "public", |
| 54 | + "url": "https://github.com/your-org/your-repo", |
| 55 | + "description": "Your module description", |
| 56 | + "applications": [ |
| 57 | + { |
| 58 | + "name": "your-app-name", |
| 59 | + "type": "web", |
| 60 | + "entrypoint": "dist/index.html" |
| 61 | + } |
| 62 | + ] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +{{% /tab %}} |
| 67 | +{{% tab name="Example" %}} |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "module_id": "acme:dashboard", |
| 72 | + "visibility": "public", |
| 73 | + "url": "https://github.com/acme/dashboard", |
| 74 | + "description": "An example dashboard for a fictitious company called Acme.", |
| 75 | + "applications": [ |
| 76 | + { |
| 77 | + "name": "dashboard", |
| 78 | + "type": "web", |
| 79 | + "entrypoint": "dist/index.html" |
| 80 | + } |
| 81 | + ] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +{{% /tab %}} |
| 86 | +{{< /tabs >}} |
| 87 | + |
| 88 | +{{% expand "Click to view" %}} |
| 89 | + |
| 90 | +<!-- prettier-ignore --> |
| 91 | +| Name | Type | Inclusion | Description | |
| 92 | +|------|------|-----------|-------------| |
| 93 | +| `module_id` | string | **Required** | The module ID, which includes the organization name and the module name. `module_id` uniquely identifies your module. | |
| 94 | +| `visibility` | string | **Required** | Must be `"public"`. | |
| 95 | +| `description` | string | **Required** | A description of your module and what it provides. | |
| 96 | +| `url` | string | Optional | The URL of the GitHub repository containing the source code of the module. | |
| 97 | +| `applications` | array | Optional | Objects that provide information about the [single page apps](/operate/reference/single-page-apps/) associated with the module. | |
| 98 | +| `models` | array | Optional | Empty unless you are shipping the app alongside models. For information on how to add models, see [Integrate other hardware](/operate/get-started/other-hardware/). | |
| 99 | + |
| 100 | +{{% /expand%}} |
| 101 | + |
| 102 | +The `applications` field is an array of application objects with the following properties: |
| 103 | + |
| 104 | +<!-- prettier-ignore --> |
| 105 | +| Property | Type | Description | |
| 106 | +| ------------ | ------ | ------------------------------------------------------------------------------------------------- | |
| 107 | +| `name` | string | The name of your application, which will be a part of the app's URL (`name.publicnamespace.viamapps.com`). For more information on valid names see [](/operate/reference/naming-modules). | |
| 108 | +| `type` | string | The type of application (currently only `"web"` is supported). | |
| 109 | +| `entrypoint` | string | The path to the HTML entry point for your application. The `entrypoint` field specifies the path to your application's entry point. For example: <ul><li><code>"dist/index.html"</code>: Static content rooted at the `dist` directory</li><li><code>"dist/foo.html"</code>: Static content rooted at the `dist` directory, with `foo.html` as the entry point</li><li><code>"dist/"</code>: Static content rooted at the `dist` directory (assumes `dist/index.html` exists)</li><li><code>"dist/bar/foo.html"</code>: Static content rooted at `dist/bar` with `foo.html` as the entry point</li></ul> | |
| 110 | + |
| 111 | +{{% /tablestep %}} |
| 112 | +{{% tablestep number=3 %}} |
| 113 | + |
| 114 | +**Package your app into a module and upload it** to the Viam Registry: |
| 115 | + |
| 116 | +TODO: first command doesn't make sense |
| 117 | + |
| 118 | +```sh {class="command-line" data-prompt="$" data-output="3-10"} |
| 119 | +viam module build local |
| 120 | +viam module upload module.tar.gz |
| 121 | +``` |
| 122 | + |
| 123 | +TODO: the upload command requires platform & version - is that no longer the case? |
| 124 | + |
| 125 | +For subsequent updates you can use: |
| 126 | + |
| 127 | +```sh {class="command-line" data-prompt="$" data-output="2-10"} |
| 128 | +viam module update |
| 129 | +``` |
| 130 | + |
| 131 | +{{% /tablestep %}} |
| 132 | +{{< /table >}} |
| 133 | + |
| 134 | +## Accessing your Single Page App |
| 135 | + |
| 136 | +Once your module with the application configuration is uploaded, your application will be available at: |
| 137 | + |
| 138 | +TODO: any extra steps? |
| 139 | + |
| 140 | +``` |
| 141 | +https://your-app-name.your-public-namespace.viamapps.com |
| 142 | +``` |
| 143 | + |
| 144 | +Users will be prompted to authenticate with their Viam credentials before accessing your application: |
| 145 | + |
| 146 | +1. User navigates to `your-app-name.your-public-namespace.viamapps.com` |
| 147 | +1. User authenticates with Viam credentials |
| 148 | +1. User selects an organization, location, and machine |
| 149 | +1. User is redirected to `your-app-name.your-public-namespace.viamapps.com/machine/{machine-id}` |
| 150 | +1. Your application is rendered with access to the selected machine |
| 151 | + |
| 152 | +## Limitations |
| 153 | + |
| 154 | +- Single page apps currently only support single-machine applications |
| 155 | +- All modules with apps must have public visibility |
| 156 | +- There is no versioning or separate deploy step; the page will always render the latest version |
| 157 | +- Browsers with cookies disabled are not supported |
| 158 | + |
| 159 | +## Security Considerations |
| 160 | + |
| 161 | +- Customer apps are stored in GCS buckets that are publicly available on the internet |
| 162 | +- Avoid uploading sensitive information in your application code or assets |
| 163 | +- API keys and secrets are stored in the browser's localStorage or sessionStorage |
| 164 | +- Single page apps authenticate users with FusionAuth |
| 165 | + |
| 166 | +## Example |
| 167 | + |
| 168 | +Here's a complete example of creating and uploading a simple React application as a Viam Single Page App: |
| 169 | + |
| 170 | +```bash |
| 171 | +# Create a new React app |
| 172 | +npx create-react-app my-viam-app |
| 173 | +cd my-viam-app |
| 174 | + |
| 175 | +# Build the app |
| 176 | +npm run build |
| 177 | + |
| 178 | +# Create a module with the app |
| 179 | +viam module create --name my-viam-app --public-namespace your-namespace |
| 180 | + |
| 181 | +# Edit meta.json to add the application configuration |
| 182 | +# Add the following to meta.json: |
| 183 | +# "visibility": "public", |
| 184 | +# "applications": [ |
| 185 | +# { |
| 186 | +# "name": "my-app", |
| 187 | +# "type": "web", |
| 188 | +# "entrypoint": "build/index.html" |
| 189 | +# } |
| 190 | +# ] |
| 191 | + |
| 192 | +# Copy the build directory to the module directory |
| 193 | +cp -r build/ path/to/module/ |
| 194 | + |
| 195 | +# Build and upload the module |
| 196 | +viam module build local |
| 197 | +viam module upload module.tar.gz |
| 198 | +``` |
| 199 | + |
| 200 | +After the module is approved, your application will be available at `https://my-app.your-public-namespace.viamapps.com`. |
0 commit comments