BroadcastWatcher
in package
implements
EventEmitterInterface
uses
EventEmitterTrait
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
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
$checking
private
?PromiseInterface
$checking
= null
$live
private
array<string, LiveBroadcast>
$live
= []
The broadcasts live now, by id.
$misses
private
array<string, int>
$misses
= []
Broadcast id => checks in a row it has been missing from.
$options
private
array<string, mixed>
$options
$running
private
bool
$running
= false
$timer
private
?TimerInterface
$timer
= null
$youtube read-only
private
YouTube
$youtube
Methods
__construct()
public
__construct(YouTube $youtube[, array<string, mixed> $options = [] ]) : mixed
Parameters
- $youtube : YouTube
- $options : array<string, mixed> = []
-
See DEFAULTS.
broadcastFor()
The broadcast whose chat this is, if it is live.
public
broadcastFor(string $liveChatId) : ?LiveBroadcast
Parameters
- $liveChatId : string
Return values
?LiveBroadcastcheck()
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
live()
The broadcasts live now, by id.
public
live() : array<string, LiveBroadcast>
Return values
array<string, LiveBroadcast>start()
Checks now, then keeps checking.
public
start() : void
stop()
Stops checking. What it knows is kept, for a later {@see start()}.
public
stop() : void
counts()
Whether a broadcast is one this watcher reports: actually live, and visible enough.
private
counts(LiveBroadcast $broadcast) : bool
Parameters
- $broadcast : LiveBroadcast
Return values
boolreconcile()
private
reconcile(LiveBroadcastListResponse $response) : void
Parameters
- $response : LiveBroadcastListResponse
scheduleNext()
private
scheduleNext() : void