spatie/simple-tcp-client
Simple TCP client for PHP/Laravel: connect to a host/port, send data, and receive responses with a clean API. Useful for interacting with TCP services (SMTP, HTTP, custom servers), testing network protocols, and building lightweight clients.
socket_* functions, reducing boilerplate for TCP operations. Aligns with Laravel’s philosophy of simplicity and convention.ext-sockets). No conflicts with Laravel’s ecosystem (e.g., no Symfony components).Mockery or Laravel’s MockFacade).ext-sockets and Laravel’s Composer-first ecosystem.nc), but less flexible.socket_*: More control but higher maintenance.spatie/simple-tcp-client.TcpClient decorator).spatie/simple-tcp-client).send()/receive() with Monolog handlers.connect()/send()/receive()/close()) without middleware.TcpClient in a decorator).class RetryableTcpClient {
public function __construct(private TcpClient $client, private int $retries) {}
public function send(string $data): string {
for ($i = 0; $i < $this->retries; $i++) {
try {
return $this->client->send($data);
} catch (Exception $e) {
if ($i === $this->retries - 1) throw $e;
sleep(1 << $i); // Exponential backoff
}
}
}
}
TcpClient as a singleton/bound service.TcpConnected, TcpError).MockFacade or Mockery to stub TcpClient in tests.stream_socket_client).TcpClient instance holds a socket; avoid instantiating per request if connections are reused.autocannon or custom scripts to identify bottlenecks (e.g., connection setup time).| Failure Type | Impact | Mitigation |
|---|---|---|
| Network Unavailable | Timeouts, failed requests | Retries with exponential backoff, circuit breakers |
| Protocol Mismatch | Malformed responses, crashes | Input validation, framing middleware |
| Connection Leaks | Memory bloat, port exhaustion | Connection pooling, close() in finally blocks |
| Remote Server Crash | Unrecoverable errors | Health checks, failover to backup servers |
| Laravel Process Crash | Unclosed sockets | Use registerShutdownFunction to ensure cleanup |
tcpdump, Wireshark).socket_* docs or Spatie’s other packages (e.g., laravel-queue for async).socat or netcatHow can I help you explore Laravel packages today?