# Getting Started with Web SDK

Depending on the use case, there are multiple ways to integrate the Web SDK.

The easiest way to get started is to use the iframe-based [Embed Player](#embed-player). It requires the least amount of code and will automatically be updated when new releases are available.

The two other alternatives are [Vindral Live Player](#vindral-player) and [Core SDK](#core-sdk) - these are the recommended options if the integration requires access to the APIs, such as timed metadata or other controls. The player is a web component-based player for easy integration and customization, while the Core SDK is the most stripped-down implementation for use cases where all UI components are customized.

If you are going for one of the SDK alternatives, follow the steps at [Installation](#installation) to get started.

## Vindral's Configurable Latency Advantage

**One of Vindral Live's key differentiators is our configurable latency system.** Unlike many other low-latency solutions in the market, Vindral allows you to fine-tune latency to match your specific requirements while maintaining superior stability.

**Why this matters:**

- **Stable Low Latency**: When you configure Vindral Live for low latency, you get more stable performance compared to protocols that enforce rigid low-latency settings. Our adaptive buffering algorithms, end-to-end jitter management, and robust CDN architecture work together to ensure smooth playback even under challenging network conditions.
- **Flexible Configuration**: Adjust `minBufferTime` from ultra-low latency (sub-second) to higher stability (several seconds) based on your use case
- **Reliability First**: Our adaptive approach ensures consistent playback quality across varying network conditions

This configurable approach means you're not locked into a one-size-fits-all latency setting - you get the best of both worlds: the ability to go as low as needed while maintaining the stability that fixed solutions can't match.

📖 **Learn more:** See our comprehensive [Latency Control](https://docs.vindral.com/playback/latency-control.md) guide for detailed configuration strategies and use case recommendations.

## Embed Player

This is the simplest way to integrate. It allows you to add a fully functional video player to your site in no time. It will also be automatically updated when improvements are released.

Add the following HTML snippet, set `core.channelId` to the *channel id* credential, and you're done.

```html
<iframe
  src="https://player.vindral.com/?core.channelId=vindral_demo1_ci_099ee1fa-80f3-455e-aa23-3d184e93e04f"
  frameborder="0"
  allow="autoplay; fullscreen"
  allowfullscreen
></iframe>
```

[See the player in action](https://player.vindral.com/?core.channelId=vindral_demo1_ci_099ee1fa-80f3-455e-aa23-3d184e93e04f) or [read more about the Embeddable Player here](https://docs.vindral.com/playback/embeddable-player.md).

## Installation

Install @vindral/web-sdk using `npm`:

```bash
npm install --save @vindral/web-sdk
```

Equivalent for `yarn`:

```bash
yarn add @vindral/web-sdk
```

### Core SDK

The Core SDK is the lowest level method to integrate that provides only a video view and audio playback - this allows for complete customization of the look of buffering indication and controls.

**Example**

The example assumes that there is an HTML page that loads this script that has at least a div with `id="root"`, `id="playback-state"` and a button with `id="activate-audio-button"`.

```javascript
import { Vindral } from "@vindral/web-sdk"

const root = document.querySelector("#root")
const button = document.querySelector("#activate-audio-button")
const playbackState = document.querySelector("#playback-state")

button.style.display = "none"

const instance = new Vindral({
  url: "https://lb.cdn.vindral.com",
  channelId: "vindral_demo1_ci_099ee1fa-80f3-455e-aa23-3d184e93e04f",
  minBufferTime: 1000, // Vindral's configurable latency: adjust from 100ms (ultra-low) to 5000ms+ (high stability)
})

// Errors are emitted when they cannot be handled internally
// fatal errors mean that the client has been unloaded and will need to be re-initialized
instance.on("error", (error) => {
  if (error.isFatal()) {
    // A fatal error has occurred and the instance has been unloaded, read error.message to see what
    // This can happen if the client has been unsuccessful to connect or authentication failed
    // In this case a new Vindral instance needs to be created to restore the session
  }
})

// This event is emitted when the playback state changes - can be used to show a buffer spinner while buffering
instance.on("playback state", (state) => (playbackState.textContent = state))

// This event is emitted when timed metadata events occur
instance.on("metadata", (metadata) => console.log("metadata: ", metadata.content))

// This event is emitted when Vindral detects that the browser requires a user-initiated click event to start the audio
instance.on("needs user input", () => (button.style.display = "block"))

// Will connect, start the stream and try to play
instance.play()

// Attaches the video view to the DOM
instance.attach(root)

// This activates audio on browsers that need user input before the audio can be played
button.addEventListener("click", () => {
  button.style.display = "none"
  instance.play()
})
```

> **Tip**
>
> If you are optimizing for the lowest possible latency or want to experiment with WebCodecs, decoder order, OffscreenCanvas rendering, or the media-element bridge for fullscreen/PiP, see [WebCodecs & Offscreen Renderer](https://docs.vindral.com/playback/websdk/webcodecs-offscreen-renderer.md).

### Vindral Player

Vindral Player is our ready-to-go web component-based player for easy integration into your web applications. It is ideal for customizing controls or when the embed solution is not sufficient.

**Example**

To be able to use the `<vindral-player />` in your HTML you have to call `registerComponents`.

```javascript
import { registerComponents } from "@vindral/web-sdk/player"

registerComponents()
```

Or use `vindral-player-component.js`, which is a self-registering variant of the web components player intended to be used from a script tag. Vindral hosts it at `https://js.vindral.com/web-sdk/<version>/`.

> **Note: Use a pinned version**
>
> There is no `latest` URL. Pick a version from the [list of hosted versions](https://js.vindral.com/web-sdk/) and change it when you upgrade.

```html
<script type="module" async src="https://js.vindral.com/web-sdk/4.8.2/vindral-player-component.js"></script>
```

Channel id is the only required attribute when using `<vindral-player />`.

```html
<vindral-player channel-id="vindral_demo1_ci_099ee1fa-80f3-455e-aa23-3d184e93e04f"></vindral-player>
```

Easy to customize controls with CSS variables.

```css
<style>
  vindral-player {
    aspect-ratio: 16 / 9;
    --vindral-fg-strong: pink;
    --vindral-mute-button-display: none;
    --vindral-pip-button-display: none;
    --vindral-fullscreen-button-display: none;
  }
</style>
```

### Player SDK

> **Deprecated**: The Player SDK is deprecated and has been replaced with the [Vindral Player](#vindral-player). It can still be used with `@vindral/web-sdk/legacy`.

**Example**

This example attaches a player to an element with the id `root`. The player will activate audio when needed and provide a minimalistic UI for controls such as fullscreen, channel switching, and language selection.

The example assumes that there is an HTML page that loads this script that has at least a div with `id="root"`.

```typescript
import { Player } from "@vindral/web-sdk/legacy"

const root = document.querySelector("#root")

const player = new Player({
  url: "https://lb.cdn.vindral.com",
  channelId: "vindral_demo1_ci_099ee1fa-80f3-455e-aa23-3d184e93e04f",
  channelGroupId: "vindral_demo_pk_932730be-db0c-46a0-a592-cfce7bdc5a43",
  minBufferTime: 1000, // Vindral's configurable latency: adjust from 100ms (ultra-low) to 5000ms+ (high stability)
})

// Will connect, start the stream and try to play
player.core.play()

// Attaches the player view to the DOM
player.attach(root)
```
