Skip to Content
JavaScript Client

JavaScript Client

Use this page if you integrate Sudden directly in JavaScript without using the React bindings.

loadSudden

loadSudden loads the Sudden script from https://js.sudden.network (if it is not already on the page) and returns the Sudden client object.

import { loadSudden } from "@sudden-network/sudden"; export const setupSudden = async () => { const sudden = await loadSudden(); await sudden?.init({ clientId: "YOUR_CLIENT_ID", serviceWorkerPath: "/sudden.service-worker.js", }); };

loadSudden behaves as follows:

  • Only loads the script once, even if you call it multiple times.
  • Resolves to null in server environments where window is not available.
  • Rejects if Sudden cannot be loaded.

Call loadSudden once near your app’s entry point and re-use the returned client instead of loading Sudden from many different places.

Sudden client

Once Sudden is loaded, you work with the Sudden client object.

init(config)

Configures Sudden for the current page.

await sudden.init({ clientId: "YOUR_CLIENT_ID", serviceWorkerPath: "/sudden.service-worker.js", });

Config options:

  • clientId – your Sudden client identifier.

  • serviceWorkerPath (optional) – the path to your self-hosted service worker loader (for example "/sudden.service-worker.js"). This file must live on your site origin and import Sudden’s service worker:

    /sudden.service-worker.js
    self.importScripts("https://js.sudden.network/service-worker.js");

    If you omit serviceWorkerPath, Sudden will not try to register a service worker for you. In that case you must integrate Sudden into your existing worker yourself (for example by calling importScripts there). See the Service worker page for details.

init can be called only once per page load. Calling it again rejects with an error.

ready

A promise that resolves when Sudden is ready to help with delivery on the page. If you already await init, you do not need to wait on ready separately.

This resolves immediately in environments where Sudden cannot run, so you can safely await it when needed.

isSupported()

Returns true when the browser has the capabilities Sudden needs.

if (!sudden.isSupported()) { // Browser is not supported, your player continues to use the CDN as usual. }

on(event, handler)

Lets you listen to high-level Sudden events.

const off = sudden.on("peerConnected", (data) => { console.log("Peer connected", data.peerId); }); // Later, to stop listening: off();

Events include:

  • "peerConnected" – a peer has connected to the viewer.
  • "peerDisconnected" – a peer has disconnected.
  • "segmentDownloaded" – a segment has been downloaded (from HTTP, cache, or P2P).
  • "segmentUploaded" – a segment has been shared with another peer.

These events are optional but can be useful for debugging or building simple telemetry and overlays in your own UI.

clearCache()

Clears the data Sudden has stored in the browser cache.

await sudden.clearCache();

This is mainly useful for debugging or resetting Sudden state during testing.

Last updated on