Replace the sidebar
The sidebar is the panel beside the desktop in a Kasm session. The runtime ships one, named default, with the Workspace, Messaging, Activity and Details tabs. You can build your own, install it in the image, and select it per session.
A sidebar is a bundle: a directory with sidebar.js and sidebar.css. Each session picks a bundle with KASM_RUNTIME_SIDEBAR=<name>. An image can carry several, and installing one leaves the stock viewer and the other bundles unchanged. Installing the same name again replaces that bundle. A sidebar needs no model.
Your bundle depends on how the viewer page hosts it (this page) and on how it talks to the runtime (the sidebar protocol). The bundled sidebar’s internal structure is available to neither: it has no slots or plugin API.
Before you start
Section titled “Before you start”- A stock
kasmweb/*image built with the SDK’sstock_image_modslayer, which includes thekasm-sidebar-installcommand (Enable the runtime on a workspace). - The workspace’s
runtimeport map. The sidebar reaches the runtime through it. - A copy of the
09-custom-sidebarexample from the toolkit: a working sidebar in plain JavaScript with no build step.
Mount the bundle in the page
Section titled “Mount the bundle in the page”sidebar.js is an ES module that the viewer page loads with <script type="module">. It mounts itself when it runs.
- Append one element to
document.body. Leave the viewer’s#noVNC_containerwhere it is. - Link the stylesheet from the module. The installer puts the CSS beside the script under the same hashed name, so derive its URL from
import.meta.urlby swapping.jsfor.css. - Scope every CSS selector under your root element.
- To take width, make
bodya flex row so#noVNC_containershrinks beside the sidebar. After any change of width, dispatch aresizeevent onwindow. KasmVNC re-fits the desktop on that event only. - Keep everything in the bundle. The viewer runs under
Cross-Origin-Embedder-Policy: require-corp, so web fonts, icon CDNs and scripts from another origin fail to load. Other files in the bundle directory are copied beside the script. Refer to them relative to the module URL.
Keep keyboard focus with the desktop
Section titled “Keep keyboard focus with the desktop”Typing in the sidebar must stay out of the session, and the session must get focus back when the person finishes.
- Give every control that is not a text input
tabindex="-1", and cancelmousedownon it withpreventDefault. - Stop
keydown,keypressandkeyupfrom propagating out of your root element. - A text input takes focus while the person types.
Escapein any input, and submitting a form, blur it and return focus to the viewer’s canvas. - Call
focus()on the sidebar only when the person clicks into an input.
Sign in to the runtime
Section titled “Sign in to the runtime”In a Kasm session every route the sidebar reads needs a token. The runtime generates one for the selected sidebar, and the sidebar gets it from the viewer.
- The runtime writes a 64-character hexadecimal token to
<sidebar root>/www/Downloads/.kasm-runtime/token. KasmVNC serves that folder at/desktop/<kasm_id>/vnc/Downloads/.kasm-runtime/token, behind the session’s Kasm sign-in, so only people signed in to view the session can fetch it. - Your sidebar fetches that URL with
cache: 'no-store'and a unique query value so no cache serves an old file, and keeps the token insessionStorage. The runtime may not be listening when the page loads: request a route such as<base>inboxwithout a token until it answers 401, which shows the runtime is listening, and fetch the file then. - Send the token in the
X-Kasm-Runtime-Session-Tokenheader on every request to the runtime, including the event stream, screenshots and attachments. The runtime refuses it in a query string. - Acknowledge with
POST <base>sidebar/authand the same header. The runtime deletes the file. If nothing acknowledges it, the file is deleted after five minutes (KASM_RUNTIME_SIDEBAR_TOKEN_TTLchanges that).
The token is published only for the selected sidebar. With KASM_RUNTIME_SIDEBAR=off, a name that is not installed, or no sidebar selected, there is nothing to fetch.
The token grants operator rights: the sidebar can read activity, message the agent and start or stop it. Everyone who can fetch the file receives it, including view-only viewers, so anyone viewing the session can read its activity and message its agent. Deleting the file stops new fetches and leaves a token already held by a browser valid.
If a request returns 401, discard the stored token and fetch the file again. After a reload, use the stored token, or ask another open tab for it over a BroadcastChannel scoped to the session. The bundled sidebar does both.
Talk to the runtime
Section titled “Talk to the runtime”The viewer page is /desktop/<kasm_id>/vnc/vnc.html, and the runtime is its sibling at /desktop/<kasm_id>/runtime/. Derive that base from the page’s own location.
- Read
GET <base>events?after=<seq>&run=<run_id>as a stream withfetchand a stream reader. The runtime can restart for another run while the page is open. Reconnect, and reset when a differentrun_idarrives. - Screenshots are at
GET <base>artifacts/<file>. - Writes to the inbox (
POST <base>messages,<base>attachments,<base>actions) name the run. Generate each write’s idempotency key before the first attempt and reuse it on retry. - Keep nothing you need after a reload in the page. The inbox and the trace live in the runtime.
Event types, request shapes and inbox rules are in the sidebar protocol reference.
Install it in the image
Section titled “Install it in the image”On top of an image built with stock_image_mods:
FROM kasmweb/chrome:stock-modsUSER rootCOPY my-bundle /tmp/my-bundleRUN kasm-sidebar-install --name acme /usr/share/kasmvnc/www /tmp/my-bundle && rm -rf /tmp/my-bundleUSER 1000kasm-sidebar-install --name acme copies the stock KasmVNC web root to /usr/share/kasmvnc-sidebars/acme/www, puts your files in its assets/ as sidebar-<hash>.js and sidebar-<hash>.css, and creates the Downloads/.kasm-runtime folder the token goes in. The stock web root and every other sidebar are untouched.
A name matches [a-z0-9][a-z0-9_-]*. KASM_SIDEBAR_ROOT changes the root directory from /usr/share/kasmvnc-sidebars.
Kasm’s proxy caches assets under /desktop/ for four hours. The installer hashes the script and stylesheet names. Give any other fonts, images or assets new names when their contents change.
Select it per session
Section titled “Select it per session”Set the variable in the workspace’s environment map or in the session request:
{ "environment": { "KASM_RUNTIME_SIDEBAR": "acme" }}KASM_RUNTIME_SIDEBAR |
Viewer served |
|---|---|
Unset, with a session token (or KASM_RUNTIME on) |
default, the runtime’s sidebar |
| Unset, in any other session | The stock viewer, no sidebar |
off |
The stock viewer, no sidebar |
| An installed name | That sidebar |
| Malformed, or not installed | The stock viewer. The reason and the installed names are logged |
A session’s sidebar is fixed for the life of the session. Every sidebar’s web root is built with the image and is read-only afterwards, so the session cannot alter the page a person loads to watch it.
Check it works
Section titled “Check it works”Start a session of the workspace with KASM_RUNTIME_SIDEBAR=acme. Your panel appears beside the desktop, the desktop re-fits beside it, and typing in the panel does not reach the session. Open the browser’s developer tools on the session page: the Network tab shows assets/sidebar-<hash>.js and the request for Downloads/.kasm-runtime/token. With off, neither loads.
The session’s log has sidebar-select: sidebar 'acme' (KASM_VNC_PATH=/usr/share/kasmvnc-sidebars/acme) when the selector found the bundle.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
Stock viewer, and no sidebar 'acme' under /usr/share/kasmvnc-sidebars (installed: ...) in the log |
The name is not installed in this image | Match the name in the Dockerfile to the variable, and use the rebuilt image |
Stock viewer, and ... is not a valid name ... |
Uppercase letters or other characters in the name | Use lowercase letters, digits, - and _, starting with a letter or digit |
Build fails with has no sidebar.js/sidebar.css |
A required file is missing | Put both files at the top of the bundle directory |
Build fails with does not look like a KasmVNC web root (no vnc.html) |
The second argument is not the stock web root | Pass /usr/share/kasmvnc/www |
| The sidebar shows no data and requests return 401 | The sidebar did not send the token, or fetched the file after it was deleted | Send the token in the header. Fetch the file once, then keep the token |
| The sidebar cannot find the token file | The runtime has not published one: the sidebar is off or not installed, or the runtime is still starting | Check the selection and wait for the runtime to answer |
| A font, icon or script does not load | It comes from another origin, which the viewer blocks | Copy it into the bundle |
| Keys typed in the sidebar also appear in the app | Key events leave the sidebar | Stop propagation on your root element |
| The desktop does not shrink beside the panel | No resize event after the width changed |
Dispatch resize on window |