|
| 1 | +--- |
| 2 | +title: "Create a Viam application" |
| 3 | +linkTitle: "Create a Viam application" |
| 4 | +weight: 5 |
| 5 | +layout: "docs" |
| 6 | +type: "docs" |
| 7 | +description: "Create and deploy a custom web interface for your machines without managing hosting and authentication." |
| 8 | +--- |
| 9 | + |
| 10 | +Create and deploy a custom web interface for your machines without managing hosting and authentication. |
| 11 | +Once deployed, your application is accessible from a dedicated URL (`appname_publicnamespace.viamapplications.com`), and hosting and authentication is handled for you. |
| 12 | + |
| 13 | +Users log into your application and select a machine they have access to. |
| 14 | +The application then renders your custom interface for interacting with the user's machine. |
| 15 | + |
| 16 | +{{<gif webm_src="/spa.webm" mp4_src="/spa.mp4" alt="Sample web application" max-width="500px">}} |
| 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 | +## Build a custom web interface |
| 32 | + |
| 33 | +You can build a custom web interface to access your machines using your preferred framework like React, Vue, Angular, or others. |
| 34 | + |
| 35 | +### Access machines from your application |
| 36 | + |
| 37 | +When logging into a Viam application and selecting a machine to use it with, the machine's API key is stored as a cookie. |
| 38 | +You can access the data from your browser's cookies as follows: |
| 39 | + |
| 40 | +```ts {class="line-numbers linkable-line-numbers" data-line=""} |
| 41 | +import Cookies from "js-cookie"; |
| 42 | + |
| 43 | +let apiKeyId = ""; |
| 44 | +let apiKeySecret = ""; |
| 45 | +let host = ""; |
| 46 | +let machineId = ""; |
| 47 | + |
| 48 | +// Extract the machine identifier from the URL |
| 49 | +const machineCookieKey = window.location.pathname.split("/")[2]; |
| 50 | +({ |
| 51 | + apiKey: { id: apiKeyId, key: apiKeySecret }, |
| 52 | + machineId: machineId, |
| 53 | + hostname: host, |
| 54 | +} = JSON.parse(Cookies.get(machineCookieKey)!)); |
| 55 | +``` |
| 56 | + |
| 57 | +### Local development |
| 58 | + |
| 59 | +For developing your application on localhost: |
| 60 | + |
| 61 | +1. Run your web server. |
| 62 | +1. Run the following command specifying the address where your app is running on localhost and a machine to test on. |
| 63 | + The command will proxy your local app and open a browser window and navigate to `http://localhost:8000/machine/<machineHostname>` for the machine provided with --machine-id. |
| 64 | + |
| 65 | + {{< tabs >}} |
| 66 | + {{% tab name="Template" %}} |
| 67 | + |
| 68 | +```sh {class="command-line" data-prompt="$" data-output="2-10"} |
| 69 | +viam login |
| 70 | +viam module local-app-testing --app-url http://localhost:<PORT> --machine-id <MACHINE-ID> |
| 71 | +``` |
| 72 | + |
| 73 | +{{% /tab %}} |
| 74 | +{{% tab name="Example" %}} |
| 75 | + |
| 76 | +```sh {class="command-line" data-prompt="$" data-output="2-10"} |
| 77 | +viam login |
| 78 | +viam module local-app-testing --app-url http://localhost:3000 --machine-id a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
| 79 | +``` |
| 80 | + |
| 81 | +{{% /tab %}} |
| 82 | +{{< /tabs >}} |
| 83 | + |
| 84 | +### Configure routing |
| 85 | + |
| 86 | +When using your deployed application, static files will be accessible at `https://your-app-name_your-public-namespace.viamapplications.com/machine/<machineHostname>/`. |
| 87 | +If your HTML file loads other files, use relative paths to ensure your files are accessible. |
| 88 | + |
| 89 | +## Deploy your web interface as a Viam application |
| 90 | + |
| 91 | +To deploy your application with Viam you must package it as a module and upload it using the Viam CLI. |
| 92 | + |
| 93 | +{{< table >}} |
| 94 | +{{% tablestep number=1 %}} |
| 95 | + |
| 96 | +**Create a <FILE>meta.json</FILE>** file for your module using this template: |
| 97 | + |
| 98 | +{{< tabs >}} |
| 99 | +{{% tab name="Template" %}} |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "module_id": "your-namespace:your-module", |
| 104 | + "visibility": "public", |
| 105 | + "url": "https://github.com/your-org/your-repo", |
| 106 | + "description": "Your module description", |
| 107 | + "applications": [ |
| 108 | + { |
| 109 | + "name": "your-app-name", |
| 110 | + "type": "single_machine", |
| 111 | + "entrypoint": "dist/index.html" |
| 112 | + } |
| 113 | + ] |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +{{% /tab %}} |
| 118 | +{{% tab name="Example" %}} |
| 119 | + |
| 120 | +```json |
| 121 | +{ |
| 122 | + "module_id": "acme:dashboard", |
| 123 | + "visibility": "public", |
| 124 | + "url": "https://github.com/acme/dashboard", |
| 125 | + "description": "An example dashboard for a fictitious company called Acme.", |
| 126 | + "applications": [ |
| 127 | + { |
| 128 | + "name": "dashboard", |
| 129 | + "type": "single_machine", |
| 130 | + "entrypoint": "dist/index.html" |
| 131 | + } |
| 132 | + ] |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +{{% /tab %}} |
| 137 | +{{< /tabs >}} |
| 138 | + |
| 139 | +This file specifies the contents of the module. |
| 140 | +It is required for your module. |
| 141 | + |
| 142 | +{{% expand "Click to view more information on attributes." %}} |
| 143 | + |
| 144 | +<!-- prettier-ignore --> |
| 145 | +| Name | Type | Inclusion | Description | |
| 146 | +|------|------|-----------|-------------| |
| 147 | +| `module_id` | string | **Required** | The module ID, which includes the organization name and the module name. `module_id` uniquely identifies your module. | |
| 148 | +| `visibility` | string | **Required** | Must be `"public"`. | |
| 149 | +| `description` | string | **Required** | A description of your module and what it provides. | |
| 150 | +| `url` | string | Optional | The URL of the GitHub repository containing the source code of the module. | |
| 151 | +| `applications` | array | Optional | Objects that provide information about the [applications](/operate/control/viam-applications/) associated with the module. | |
| 152 | +| `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/). | |
| 153 | + |
| 154 | +{{% /expand%}} |
| 155 | + |
| 156 | +The `applications` field is an array of application objects with the following properties: |
| 157 | + |
| 158 | +<!-- prettier-ignore --> |
| 159 | +| Property | Type | Description | |
| 160 | +| ------------ | ------ | ----------- | |
| 161 | +| `name` | string | The name of your application, which will be a part of the application's URL (`name_publicnamespace.viamapplications.com`). For more information on valid names see [Valid application identifiers](/operate/reference/naming-modules/#valid-application-identifiers). | |
| 162 | +| `type` | string | The type of application (currently only `"single_machine"` is supported). | |
| 163 | +| `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> | |
| 164 | + |
| 165 | +{{% /tablestep %}} |
| 166 | +{{% tablestep number=2 %}} |
| 167 | +**Register your module** with Viam: |
| 168 | + |
| 169 | +{{< tabs >}} |
| 170 | +{{% tab name="Template" %}} |
| 171 | + |
| 172 | +```sh {class="command-line" data-prompt="$" data-output="3-10"} |
| 173 | +viam module create --name="app-name" --public-namespace="namespace" |
| 174 | +``` |
| 175 | + |
| 176 | +{{% /tab %}} |
| 177 | +{{% tab name="Example" %}} |
| 178 | + |
| 179 | +```sh {class="command-line" data-prompt="$" data-output="3-10"} |
| 180 | +viam module create --name="air-quality" --public-namespace="naomi" |
| 181 | +``` |
| 182 | + |
| 183 | +{{% /tab %}} |
| 184 | +{{< /tabs >}} |
| 185 | + |
| 186 | +{{% /tablestep %}} |
| 187 | +{{% tablestep number=3 %}} |
| 188 | + |
| 189 | +**Package your static files and your <FILE>meta.json</FILE> file and upload them** to the Viam Registry: |
| 190 | + |
| 191 | +```sh {class="command-line" data-prompt="$" data-output="3-10"} |
| 192 | +tar -czvf module.tar.gz <static-website-files> meta.json |
| 193 | +viam module upload --upload=module.tar.gz --platform=any --version=0.0.1 |
| 194 | +``` |
| 195 | + |
| 196 | +For subsequent updates run these commands again with an updated version number. |
| 197 | + |
| 198 | +{{% /tablestep %}} |
| 199 | +{{< /table >}} |
| 200 | + |
| 201 | +## Access your application |
| 202 | + |
| 203 | +After uploading your module with the application configuration, your application will be available at: |
| 204 | + |
| 205 | +```txt |
| 206 | +https://your-app-name_your-public-namespace.viamapplications.com |
| 207 | +``` |
| 208 | + |
| 209 | +Users will be prompted to authenticate with their Viam credentials before accessing your application: |
| 210 | + |
| 211 | +1. User navigates to `your-app-name_your-public-namespace.viamapplications.com`. |
| 212 | +1. User authenticates with Viam credentials. |
| 213 | +1. User selects an organization, location, and machine. |
| 214 | +1. User is redirected to `your-app-name_your-public-namespace.viamapplications.com/machine/<machineHostname>`. |
| 215 | +1. Your application is rendered with access to the selected machine. |
| 216 | + The credentials for that one machine are provided in the cookies. |
| 217 | + |
| 218 | +## Example |
| 219 | + |
| 220 | +<!-- For a TypeScript example see [Monitor Air Quality with a Fleet of Sensors](/tutorials/control/air-quality-fleet/). --> |
| 221 | + |
| 222 | +For a React application that shows camera feeds for a machine, see [Viam Camera Viewer](https://github.com/viam-labs/viam-camera-viewer). |
| 223 | + |
| 224 | +## Limitations |
| 225 | + |
| 226 | +- Applications currently only support single-machine applications. |
| 227 | +- All modules with applications must have public visibility. |
| 228 | +- The page will always render the latest version. |
| 229 | +- Browsers with cookies disabled are not supported. |
| 230 | +- Viam applications serve static files. |
| 231 | + If you are building an application with server-side rendering or need other back-end capabilities, Viam applications is not the right choice. |
| 232 | + |
| 233 | +## Security considerations |
| 234 | + |
| 235 | +- Customer applications are stored publicly on the internet, so avoid uploading sensitive information in your application code or assets. |
| 236 | +- API keys and secrets are stored in the browser's cookies. |
| 237 | +- Users authenticate with FusionAuth. |
| 238 | + |
| 239 | +## FAQ |
| 240 | + |
| 241 | +### Can I use a custom domain? |
| 242 | + |
| 243 | +Viam does not currently support using custom domains (for example: `app.mycustomdomain.com/machine/<machineHostname>`) to serve your Viam application. |
| 244 | +You can, however, redirect from your domain to your Viam application (`app.mycustomdomain.com` -> `your-app-name_your-public-namespace.viamapplications.com`). |
| 245 | +You can configure a redirect (HTTP 301) on your web server or hosting provider from `app.mycustomdomain.com/*` to `your-app-name_your-public-namespace.viamapplications.com/*`. |
0 commit comments