ResponseStream
extends EventEmitter
in package
implements
ReadableStreamInterface
The body of a server-streamed call, as a stream of parts.
Google serves a streamed method over plain HTTP as one JSON array whose elements arrive as the server produces them, possibly hours apart. This reads the array as it comes and emits each element, hydrated, as soon as it is complete:
$youtube->liveChatMessages->stream(liveChatId: $id, part: ['snippet', 'authorDetails'])
->then(function (ResponseStream $stream) {
$stream->on('data', fn (LiveChatMessageListResponse $page) => ...);
$stream->on('close', fn () => print("the server closed the stream\n"));
});
Events, as for any readable stream: data with each element, error with
an HttpException when the server reports one mid-stream or the body
is not a JSON array, end when the array closes normally, and close
after either.
Tags
Table of Contents
Interfaces
- ReadableStreamInterface
Properties
- $closed : bool
- $depth : int
- Nesting depth inside the current element; 0 between elements.
- $element : string
- The current element, so far.
- $endpoint : string
- $escaped : bool
- $finished : bool
- Whether the array's closing `]` has been read.
- $hydrate : Closure
- $inString : bool
- $source : ReadableStreamInterface
- $started : bool
- Whether the array's opening `[` has been read.
Methods
- __construct() : mixed
- close() : void
- isReadable() : bool
- pause() : void
- pipe() : WritableStreamInterface
- resume() : void
- emitElement() : void
- end() : void
- fail() : void
- feed() : void
- Reads the next piece of the body, emitting every element it completes.
Properties
$closed
private
bool
$closed
= false
$depth
Nesting depth inside the current element; 0 between elements.
private
int
$depth
= 0
$element
The current element, so far.
private
string
$element
= ''
$endpoint read-only
private
string
$endpoint
= ''
$escaped
private
bool
$escaped
= false
$finished
Whether the array's closing `]` has been read.
private
bool
$finished
= false
$hydrate read-only
private
Closure
$hydrate
$inString
private
bool
$inString
= false
$source read-only
private
ReadableStreamInterface
$source
$started
Whether the array's opening `[` has been read.
private
bool
$started
= false
Methods
__construct()
public
__construct(ReadableStreamInterface $source, callable(array<string, mixed>): mixed $hydrate[, string $endpoint = '' ]) : mixed
Parameters
- $source : ReadableStreamInterface
- $hydrate : callable(array<string, mixed>): mixed
-
Turns one decoded element into what
datacarries. - $endpoint : string = ''
-
The API method, for error messages.
close()
public
close() : void
isReadable()
public
isReadable() : bool
Return values
boolpause()
public
pause() : void
pipe()
public
pipe(WritableStreamInterface $dest[, array<string, mixed> $options = [] ]) : WritableStreamInterface
Parameters
- $dest : WritableStreamInterface
- $options : array<string, mixed> = []
Return values
WritableStreamInterfaceresume()
public
resume() : void
emitElement()
private
emitElement() : void
end()
private
end() : void
fail()
private
fail(HttpException $e) : void
Parameters
- $e : HttpException
feed()
Reads the next piece of the body, emitting every element it completes.
private
feed(string $chunk) : void
Parameters
- $chunk : string