Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • BufferedSink
  • CompositeStream
  • DuplexResourceStream
  • ReadableResourceStream
  • ReadableStream
  • ThroughStream
  • Util
  • WritableResourceStream
  • WritableStream

Interfaces

  • DuplexStreamInterface
  • ReadableStreamInterface
  • WritableStreamInterface
  • Overview
  • Namespace
  • Class
  • Tree

Class BufferedSink

The WritableStreamInterface is responsible for providing an interface for write-only streams and the writable side of duplex streams.

Besides defining a few methods, this interface also implements the EventEmitterInterface which allows you to react to certain events:

drain event: The drain event will be emitted whenever the write buffer became full previously and is now ready to accept more data.
`php $stream->on('drain', function () use ($stream) { echo 'Stream is now ready to accept more data'; }); `
This event SHOULD be emitted once every time the buffer became full previously and is now ready to accept more data. In other words, this event MAY be emitted any number of times, which may be zero times if the buffer never became full in the first place. This event SHOULD NOT be emitted if the buffer has not become full previously.
This event is mostly used internally, see also write() for more details.

pipe event: The pipe event will be emitted whenever a readable stream is pipe()d into this stream. The event receives a single ReadableStreamInterface argument for the source stream.
`php $stream->on('pipe', function (ReadableStreamInterface $source) use ($stream) { echo 'Now receiving piped data';
// explicitly close target if source emits an error $source->on('error', function () use ($stream) { $stream->close(); }); });
$source->pipe($stream); `
This event MUST be emitted once for each readable stream that is successfully piped into this destination stream. In other words, this event MAY be emitted any number of times, which may be zero times if no stream is ever piped into this stream. This event MUST NOT be emitted if either the source is not readable (closed already) or this destination is not writable (closed already).
This event is mostly used internally, see also pipe() for more details.

error event: The error event will be emitted whenever an error occurs, usually while trying to write to this stream. The event receives a single Exception argument for the error instance.
`php $stream->on('error', function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); `
This event MAY be emitted any number of times, which should be zero times if this is a stream that is successfully terminated. It SHOULD be emitted whenever the stream detects an error, such as a transmission error. It SHOULD NOT be emitted after a close event.
Many common streams (such as a TCP/IP connection or a file-based stream) only deal with data transmission and may choose to only emit this for a fatal transmission error once and will thus likely close (terminate) the stream in response. If this is a fatal error that results in the stream being closed, it SHOULD be followed by a close event.
Other higher-level protocols may choose to keep the stream alive after this event, if they can recover from an error condition.
If this stream is a DuplexStreamInterface, you should also notice how the readable side of the stream also implements an error event. In other words, an error may occur while either reading or writing the stream which should result in the same error processing.

close event: The close event will be emitted once the stream closes (terminates).
`php $stream->on('close', function () { echo 'CLOSED'; }); `
This event SHOULD be emitted once or never at all, depending on whether the stream ever terminates. It SHOULD NOT be emitted after a previous close event.
After the stream is closed, it MUST switch to non-writable mode, see also isWritable().
This event SHOULD be emitted whenever the stream closes, irrespective of whether this happens implicitly due to an unrecoverable error or explicitly when either side closes the stream.
Many common streams (such as a TCP/IP connection or a file-based stream) will likely choose to emit this event after flushing the buffer from the end() method, after receiving a successful end event or after a fatal transmission error event.
If this stream is a DuplexStreamInterface, you should also notice how the readable side of the stream also implements a close event. In other words, after receiving this event, the stream MUST switch into non-writable AND non-readable mode, see also isReadable(). Note that this event should not be confused with the end event.

Evenement\EventEmitter implements Evenement\EventEmitterInterface uses Evenement\EventEmitterTrait
Extended by React\Stream\WritableStream implements React\Stream\WritableStreamInterface
Extended by React\Stream\BufferedSink implements React\Promise\PromisorInterface
Namespace: React\Stream
See: EventEmitterInterface
See: React\Stream\DuplexStreamInterface
Located at BufferedSink.php
Methods summary
public
# __construct( )
public
# handlePipeEvent( mixed $source )
public
# handleErrorEvent( mixed $e )
public boolean
# write( mixed|string $data )

Write some data into the stream.

Write some data into the stream.

A successful write MUST be confirmed with a boolean true, which means that either the data was written (flushed) immediately or is buffered and scheduled for a future write. Note that this interface gives you no control over explicitly flushing the buffered data, as finding the appropriate time for this is beyond the scope of this interface and left up to the implementation of this interface.

Many common streams (such as a TCP/IP connection or file-based stream) may choose to buffer all given data and schedule a future flush by using an underlying EventLoop to check when the resource is actually writable.

If a stream cannot handle writing (or flushing) the data, it SHOULD emit an error event and MAY close() the stream if it can not recover from this error.

If the internal buffer is full after adding $data, then write() SHOULD return false, indicating that the caller should stop sending data until the buffer drains. The stream SHOULD send a drain event once the buffer is ready to accept more data.

Similarly, if the the stream is not writable (already in a closed state) it MUST NOT process the given $data and SHOULD return false, indicating that the caller should stop sending data.

The given $data argument MAY be of mixed type, but it's usually recommended it SHOULD be a string value or MAY use a type that allows representation as a string for maximum compatibility.

Many common streams (such as a TCP/IP connection or a file-based stream) will only accept the raw (binary) payload data that is transferred over the wire as chunks of string values.

Due to the stream-based nature of this, the sender may send any number of chunks with varying sizes. There are no guarantees that these chunks will be received with the exact same framing the sender intended to send. In other words, many lower-level protocols (such as TCP/IP) transfer the data in chunks that may be anywhere between single-byte values to several dozens of kilobytes. You may want to apply a higher-level protocol to these low-level data chunks in order to achieve proper message framing.

Parameters

$data
mixed|string
$data

Returns

boolean

Overrides

React\Stream\WritableStream::write()
public
# close( )

Closes the stream (forcefully).

Closes the stream (forcefully).

This method can be used to forcefully close the stream, i.e. close the stream without waiting for any buffered data to be flushed. If there's still data in the buffer, this data SHOULD be discarded.

`php $stream->close(); `

Once the stream is closed, it SHOULD emit a close event. Note that this event SHOULD NOT be emitted more than once, in particular if this method is called multiple times.

After calling this method, the stream MUST switch into a non-writable mode, see also isWritable(). This means that no further writes are possible, so any additional write() or end() calls have no effect.

`php $stream->close(); assert($stream->isWritable() === false);

$stream->write('nope'); // NO-OP $stream->end(); // NO-OP `

Note that this method should not be confused with the end() method. Unlike the end() method, this method does not take care of any existing buffers and simply discards any buffer contents. Likewise, this method may also be called after calling end() on a stream in order to stop waiting for the stream to flush its final data.

`php $stream->end(); $loop->addTimer(1.0, function () use ($stream) { $stream->close(); }); `

If this stream is a DuplexStreamInterface, you should also notice how the readable side of the stream also implements a close() method. In other words, after calling this method, the stream MUST switch into non-writable AND non-readable mode, see also isReadable().

See

React\Stream\ReadableStreamInterface::close()

Overrides

React\Stream\WritableStream::close()
public
# promise( )
public static
# createPromise( React\Stream\ReadableStreamInterface $stream )
Methods inherited from React\Stream\WritableStream
end(), isWritable()
Methods inherited from Evenement\EventEmitterInterface
emit(), listeners(), on(), once(), removeAllListeners(), removeListener()
Methods used from Evenement\EventEmitterTrait
(), (), (), (), (), ()
Properties inherited from React\Stream\WritableStream
$closed
Properties used from Evenement\EventEmitterTrait
$listeners
Ratchet API documentation generated by ApiGen 2.8.0