Skip to content

Show a live stream

When a live channel is running and routed to the surface a hero asks for, the hero’s stream says so, and the SDK can show the channel’s player. The same happens when a campaign carries a live block of its own.

Two parts do the work. Hero takes a live prop, and Live is the player’s slot as a component you place yourself. Both ship in the release after 0.15.0.

<Hero surfaceId="3f2b8c1e-5a47-4d0e-9b6a-2c8e7f1d4a90" fallback={copy} live />

With live, the hero has a 16:9 slot after the body. The slot is there from the first frame, in the server HTML too, whether or not a channel is running. When the stream reports a running channel, the player fills the slot. Nothing on the page moves, because the space was already taken.

Without live, a hero never shows a player and keeps no space for one. That is the default, so an existing page does not change when you upgrade.

A live channel is routed to a surface, so pass surfaceId. A campaignId shows a player only when that campaign carries its own live block.

useIsland puts the live block on state.live. Hand it to Live wherever the player belongs in your markup:

import { Live, StreamText, useIsland } from "@sparkletree/react";
function LiveEvent({ surfaceId }: { surfaceId: string }) {
const { state, ref } = useIsland({
surfaceId,
island: "hero",
fallback: { headline: "Tonight we open the kiln" },
});
return (
<section ref={ref} className="event">
<StreamText field={state.fields.headline} as="h1" />
<Live live={state.live} className="event-player">
Not live right now
</Live>
</section>
);
}
Prop What it does
live The block to play: state.live from useIsland, or parts.live in a Hero layout. null keeps the slot empty.
children Shown in the slot while there is no player: a poster, a line of text, or nothing.
className, style Passed to the slot. style is merged over the slot’s own, so another ratio or a fixed size is yours to set.

A Hero layout gets the same block as parts.live:

<Hero
surfaceId={surfaceId}
fallback={copy}
layouts={{
split: ({ headline, body, live }) => (
<div className="split">
<div>{headline}{body}</div>
<Live live={live} />
</div>
),
}}
/>

The live prop does not apply to a layout you wrote: you place the player.

Live always renders its slot, a <div class="st-live"> with aspect-ratio: 16 / 9 set inline. The player is an iframe that fills it. The slot’s width comes from your layout, and its height follows from the ratio, so the space is reserved before any player exists.

While no channel is running the slot stays, empty or showing its children. It does not collapse, because collapsing would move the page. If you would rather have no slot at all when nothing is live, render Live only when there is a block, and accept that the page moves when the player appears:

{state.live ? <Live live={state.live} /> : null}

Style the two states with the slot’s attributes:

.st-live { background: #111; border-radius: 10px; overflow: hidden; }
.st-live[data-live="waiting"] { /* no player yet, or none running */ }
.st-live[data-live="playing"] { /* the player is in the slot */ }

Hero adds the class st-hero-live to its slot. An IslandReservation for a hero takes the same live prop, so a Suspense fallback holds the slot too.

The player is kept by its channel. If the hero asks again during the same view of the page (the visitor changed something, or your context changed) and the same channel is still running, the iframe on the page is left exactly as it is. It does not reload and playback does not restart.

The next stream says What happens
The same channel Nothing. The iframe stays.
Another channel One new player replaces the old one, in the same slot.
No live block The player goes. The slot stays.
Nothing, because it failed The player that was playing stays.

The stream describes the player as markup. The SDK does not insert that markup. It reads the player’s address and title out of it and builds the iframe itself, and only for an address of this exact shape:

https://<one label>.cloudflarestream.com/<letters and digits>/iframe

Of the address’s query, only the player switches autoplay, muted and controls are kept. Anything else in the markup is dropped. An address that does not match yields no player at all.

The iframe is a Cloudflare Stream player, so it loads from Cloudflare and the visitor’s browser connects to Cloudflare to play it. It starts muted, which is the only way browsers allow a video to start by itself.

If you run a Content Security Policy, allow the player’s frame:

frame-src https://*.cloudflarestream.com

openStream reports the same block on state.live: { channelId, src, title }, or null. Build your iframe from src, and keep it while channelId stays the same. To carry a playing channel into the next stream, pass the block you have as initialLive:

const next = openStream({ target, initial, initialLive: previous.live });

parseLiveReady and safeLiveSrc are exported from @sparkletree/core if you read the stream yourself.

The cookbook’s Live stream recipe (#/live) runs with no backend. See Run the example apps.