Embed Player Messaging API
The Sporfie player embedded in an iframe and the page around it talk to each other with the postMessage() API. Two messages exist:
sporfie:request-absolute-timestamp: your page asks the player for the absolute timestamp of the content it is playing.sporfie:playback-ended: the player tells your page that the video is over, so you can show a countdown, change page or load the next video.
Absolute timestamp
Your page sends a request to the iframe, and the player replies with the absolute timestamp of the content on screen.
Type Definitions
type TimestampRequest = { version: 1; type: "sporfie:request-absolute-timestamp"; requestId?: string;}; type TimestampResponse = { version: 1; type: "sporfie:absolute-timestamp"; requestId?: string; eventKey: string | null; absoluteTimestampMs: number | null; absoluteTimestampIso: string | null; error?: "not-ready" | "unresolved"; emittedAtMs: number;};Implementation Example
const iframe = document.querySelector("iframe");const sporfieOrigin = "https://www.sporfie.com"; // requestId is optional – set it if you have multiple embeds, repeated requests.const requestId = `request-${Date.now()}`; const request = { version: 1, type: "sporfie:request-absolute-timestamp", requestId,}; iframe?.contentWindow?.postMessage(request, sporfieOrigin); window.addEventListener("message", (event) => { if (event.origin !== sporfieOrigin) return; const data = event.data as TimestampResponse; if (data.type !== "sporfie:absolute-timestamp") return; // Only check requestId if you sent one. if (request.requestId && data.requestId !== request.requestId) return; console.log("Timestamp ms:", data.absoluteTimestampMs); console.log("Timestamp ISO:", data.absoluteTimestampIso); console.log("Error:", data.error ?? null);});Playback ended
The player posts this message to the page that hosts the iframe when the video reaches its end and nothing else is about to play. There is nothing to request and nothing to subscribe to: a message listener is the whole callback.
Type Definition
type PlaybackEndedEvent = { version: 1; type: "sporfie:playback-ended"; eventKey: string | null; item: string | null; emittedAtMs: number;};item says what ended, with the values of the item URL option: a clip key, highlights or a highlight reel key. null is the event itself (its replay).
Countdown Example
A countdown shown by your page when the video ends, followed by the next video, as on a playlist page:
const iframe = document.querySelector("iframe");const upNext = document.querySelector("#up-next");const sporfieOrigin = "https://www.sporfie.com"; // What plays next is your decision: any embed URL, or a page of your site.const nextEmbedUrl = "https://www.sporfie.com/embed/-Oab1cD2eF?item=<clipKey>&autoplay=true"; window.addEventListener("message", (event) => { if (event.origin !== sporfieOrigin) return; if (event.source !== iframe.contentWindow) return; const data = event.data as PlaybackEndedEvent; if (data.type !== "sporfie:playback-ended") return; let secondsLeft = 5; upNext.textContent = `Next video in ${secondsLeft}`; upNext.hidden = false; const timer = setInterval(() => { secondsLeft -= 1; upNext.textContent = `Next video in ${secondsLeft}`; if (secondsLeft > 0) return; clearInterval(timer); upNext.hidden = true; // Load the next video in the same iframe... iframe.src = nextEmbedUrl; // ...or change page: window.location.href = nextPageUrl; }, 1000);});When It Is Sent
- At the end of a replay: its last recording played to the end.
- At the end of a clip or of the highlights reel, when nothing follows it. That is the default for an
itemopened on an/embed/URL, which plays alone. - Not while the player carries on by itself: with
loop=true, between the rates of aspeedlist (the message follows the last one), whenautoplayNextClipmoves on to the next clip, or when the replay resumes behind a clip the viewer opened. - Not at the end of a sponsor ad, and not when a live stream stops.
- Once per play-through: a viewer who plays the video again gets a new message at its end.
Implementation Notes
- Use an explicit Sporfie origin in
postMessage, never"*", and checkevent.originon every message you receive. It is the origin of your iframe'ssrc - The iframe replies to a timestamp request only on
event.source, usingevent.origin requestIdis optional, but useful for matching responseserror: "not-ready"means the player is not ready yeterror: "unresolved"means the player cannot safely map the current playback position to a real absolute time- With several players on one page,
event.sourcetells which iframe a message comes from - For the next video to start by itself, add
autoplay=trueto its URL - The classic player (
/event/:eventKey/embed/classic, retiring at the end of 2026) answers timestamp requests but does not sendsporfie:playback-ended