YouTubePHP Documentation

BroadcastWatcher
in package
implements EventEmitterInterface uses EventEmitterTrait

FinalYes

Notices when the signed-in channel goes live, and when it stops.

YouTube has no push notification for this that a bot can use, so it asks: liveBroadcasts.list with broadcastStatus=active, one unit of quota a check. That is every two minutes while nothing is live, about 720 units a day, and every five while something is, when the chat reader usually notices the end first and says so through ended().

Both scheduled events and the streamer's "Go live now" stream are found: the check asks for broadcastType=all, where YouTube's default leaves the second out.

Events:

Event Arguments When
broadcast.live LiveBroadcast A broadcast went live. Its snippet.liveChatId is its chat.
broadcast.ended LiveBroadcast It is over: missing from two checks in a row, or ended().
error Throwable A check failed. It changes nothing; the next one tries again.

A check skipped for quota leaves room in the reserve for what a person asks for; see Meter::canAfford().

Tags
since
1.0.0
author

Valithor Obsidion valithor@valgorithms.com

Table of Contents

Interfaces

EventEmitterInterface

Constants

DEFAULTS  : mixed = [ // Seconds between checks while nothing is li...
ENDPOINT  : mixed = 'liveBroadcasts.list'
The API method each check calls.

Properties

$checking  : ?PromiseInterface
$live  : array<string, LiveBroadcast>
$misses  : array<string, int>
$options  : array<string, mixed>
$running  : bool
$timer  : ?TimerInterface
$youtube  : YouTube

Methods

__construct()  : mixed
broadcastFor()  : ?LiveBroadcast
The broadcast whose chat this is, if it is live.
check()  : PromiseInterface<string|int, array<string, LiveBroadcast>>
One check, now.
ended()  : void
A broadcast is over, as its chat reader saw: forget it now rather than after two more checks.
live()  : array<string, LiveBroadcast>
The broadcasts live now, by id.
start()  : void
Checks now, then keeps checking.
stop()  : void
Stops checking. What it knows is kept, for a later {@see start()}.
counts()  : bool
Whether a broadcast is one this watcher reports: actually live, and visible enough.
reconcile()  : void
scheduleNext()  : void

Constants

DEFAULTS

public mixed DEFAULTS = [ // Seconds between checks while nothing is live. 'interval' => 120.0, // Seconds between checks while something is. 'live_interval' => 300.0, // Checks in a row a broadcast must be missing from before it counts as ended. 'ended_after' => 2, // Also report broadcasts that are live but unlisted or private. 'include_unlisted' => true, ]

ENDPOINT

The API method each check calls.

public mixed ENDPOINT = 'liveBroadcasts.list'

Properties

$misses

private array<string, int> $misses = []

Broadcast id => checks in a row it has been missing from.

Methods

__construct()

public __construct(YouTube $youtube[, array<string, mixed> $options = [] ]) : mixed
Parameters
$youtube : YouTube
$options : array<string, mixed> = []

See DEFAULTS.

check()

One check, now.

public check() : PromiseInterface<string|int, array<string, LiveBroadcast>>
Return values
PromiseInterface<string|int, array<string, LiveBroadcast>> —

What is live, by id. Never rejects: a failed check emits error and changes nothing.

ended()

A broadcast is over, as its chat reader saw: forget it now rather than after two more checks.

public ended(string $broadcastId) : void
Parameters
$broadcastId : string

stop()

Stops checking. What it knows is kept, for a later {@see start()}.

public stop() : void
On this page

Search results