Publish a camera from a browser, then watch it through a MoQT relay.
In an empty directory, install Vite and the SDK from its GitHub release:
npm install -D vite
npm install https://github.com/nttcom/moq-wasm/releases/download/js-sdk-v0.6.0/moq-wasm-0.6.0.tgz
In vite.config.mjs, exclude the SDK from pre-bundling so Vite can load its
WebAssembly file and workers:
export default { optimizeDeps: { exclude: ['moq-wasm'] } }
Create index.html:
<button id="publish">Publish camera</button>
<video autoplay muted playsinline></video>
<output></output>
<script type="module" src="/main.js"></script>
Create main.js. This example publishes video on the public demo relay.
Anyone with the namespace can watch while the page is open.
import { MoqSession } from 'moq-wasm'
import { createBroadcast } from 'moq-wasm/media'
const button = document.querySelector('#publish')
button.onclick = async () => {
button.disabled = true
const session = await MoqSession.connect('https://relay-1.moqt.research.skyway.io:443')
const namespace = ['anon', crypto.randomUUID()]
const broadcast = await createBroadcast(session, namespace)
const camera = await broadcast.addSource('camera', { video: true })
document.querySelector('video').srcObject = camera.stream
document.querySelector('output').textContent = namespace.join('/')
}
Run npx vite, open the local URL in Chrome, click Publish camera and allow
camera access. Your preview and namespace appear. To see the stream from a
subscriber, open Live Viewer,
select relay-1, enter that namespace and click Watch. Close the publisher tab to stop.
LivePlayer.start() from a click so the browser
allows audio playback. See the player methods
and player options.watchBroadcasts() to discover participants and display their sources.The moq-wasm/media API may change in any minor release.