Connections and flow control
TcpConnection represents one TCP connection. HTTP, Text and WebSocket use this transport with different codecs. AsyncTcpConnection creates outbound connections.
Sending and closing
Section titled “Sending and closing”send($data, $raw = false): ?bool calls protocol encode unless raw=true. true means success on the local send path, not confirmation that the remote application processed the message. false indicates an error/closed connection; null indicates queued data or empty codec output. Data may remain buffered until TCP/TLS establishment or socket availability.
close($data = null, $raw = false) sends remaining data and closes after the buffer drains. end() handles write completion with linger/half-close. destroy() releases the socket immediately. During HTTP file streaming, close/end are deferred through stream lifecycle.
Backpressure
Section titled “Backpressure”getSendBufferQueueSize(), getRecvBufferQueueSize() and bufferIsEmpty() inspect queues. maxSendBufferSize and maxPackageSize limit a connection; static defaults apply when it is created.
Stop producing data on onBufferFull and resume on onBufferDrain. pauseRecv()/resumeRecv() control reading. Never busy-loop on send when the buffer is full.
$source->pipe($destination) assigns source onMessage/onClose and destination onBufferFull/onBufferDrain callbacks, replacing previous assignments. A bidirectional relay pipes both directions; authorization and destination policy belong to the application.
Outbound connections
Section titled “Outbound connections”new AsyncTcpConnection($remoteAddress, $socketContext = [], $eventLoop = null) creates the object; connect() starts establishment. setConnectTimeout($seconds) limits it. reconnect($after) schedules another attempt; cancelReconnect() cancels it. Clean up old timers/subscriptions before registering application logic again.
setIdleTimeout(), setFrameTimeout() and setTlsHandshakeTimeout() bound TcpConnection stages. Set a separate application request deadline: an active connection may never return the required answer.