Your own web UI

A workspace's app is made of HTML and JavaScript pages. The engine serves them at /app, and a page talks to the graph's input and output ports over WebSocket. On this page you drive Gain and Counter, the nodes added in Writing nodes and plugins, from a page.

Where the pages go

An engine started with the template as --vnf serves the files in templates/default/flows/default.vnf/app/ under http://localhost:8000/app/. The template's index.html is Signal Lab, which you opened in Getting started.

nonoha-client.iife.js in the same folder is the client that talks to the engine (nonoha-client-js), bundled into one file for a <script> tag.

The graph's input and output ports

A page works with the graph's own input and output ports (inputs and outputs in graph.json). Besides the output port count from Counter, add an input port gain and connect it to Gain's property port @gain, so the page can change the gain. Stop the engine and add this to templates/default/flows/default.vnf/graphs/app.vng/graph.json:

JSON
"inputs": { "gain": { "types": ["f64"] } },
"edges": [{ "source": ".gain", "target": "Gain_1.@gain" }]

These are only the parts to add: keep the inputs and edges that are there.

Write a page

Create templates/default/flows/default.vnf/app/gain.html.

HTML
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Gain</title>
<p>Count: <output id="count">-</output></p>
<p>Peak: <output id="peak">-</output></p>
<label>Gain <input id="gain" type="range" min="0" max="1" step="0.01" value="0.5"></label>
<script src="nonoha-client.iife.js"></script>
<script>
  const client = new NonohaClient.GraphClient(location.host, "app")

  // Each output of the graph arrives here, by its name
  client.addOutputCallback(({ portName, values }) => {
    const array = values[0].array // a typed array, reused for the next output
    if (portName === "count") document.getElementById("count").textContent = array[0]
    if (portName === "waveform") document.getElementById("peak").textContent = Math.max(...array).toFixed(2)
  })
  client.connect()

  // Send a value to an input of the graph
  const send = (portName, dataType, array) =>
    client.dispatchInput({ portName, values: [{ dataType, shape: [1], array }] })

  // The socket opens a moment after connect(): try until it takes the value
  function start() {
    try {
      send("enabled", NonohaClient.PortDataType.Bool, new Uint8Array([1]))
    } catch {
      setTimeout(start, 200)
    }
  }
  start()

  document.getElementById("gain").addEventListener("input", (event) => {
    send("gain", NonohaClient.PortDataType.Float64, new Float64Array([Number(event.target.value)]))
  })
</script>
</html>
  • new NonohaClient.GraphClient(location.host, "app") connects to the app graph of the engine that served the page.
  • The function you give addOutputCallback is called every time an output of the graph arrives. values[0].array is a typed array (such as Float32Array) that may be reused for the next output: copy it if you keep the values.
  • dispatchInput sends a value to an input port of the graph. dataType is the port's data type and array the value. shape starts with the batch dimension: [1] for one value, [1, 3] for an f32[3] input port.
  • The WebSocket opens a moment after connect(), and dispatchInput throws until then. As the template does, retry until the send succeeds.
  • Clock does not run until true arrives at the input port enabled.

Start the engine again and open http://localhost:8000/app/gain.html: Count goes up, and Peak shows the waveform's maximum. Move the slider to 0.2, and Peak shows 0.20.

Open it from another machine

By default the engine accepts connections from your own machine only. To open the page from a tablet or another computer on the same network, start the engine with --host 0.0.0.0. The engine's API has no authentication, so use it only on networks you trust (Using it safely).

What to read next

  • For a page that draws a graph's outputs, see the template's index.html, which draws the waveform and the spectrum on a canvas.
  • nonoha-engine's API reference describes the WebSocket and REST APIs.