Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Socket Client

Socket Client Laravel Package

php-http/socket-client

PSR-7/PSR-18 HTTP client built on PHP streams. Supports TCP and UNIX domain sockets, TLS/SSL encryption, and client certificates. Lightweight, dependency-minimal option for making HTTP requests via socket connections.

View on GitHub
Deep Wiki
Context7

Socket HTTP Client

Frequently asked questions about Socket Client
How do I integrate php-http/socket-client into a Laravel application for direct TCP/TLS communication?
Use Laravel’s service container to bind the socket client as a singleton. Register it in a service provider (e.g., `SocketClientServiceProvider`) and inject it into controllers or services. For example, bind it with a timeout and retry configuration, then expose it via a facade or dependency injection for consistency. Avoid mixing it with Laravel’s built-in `HttpClient` unless you’re handling hybrid use cases.
What Laravel versions and PHP requirements does php-http/socket-client support?
The package requires PHP 8.1+ and is tested on Laravel 10+. Earlier Laravel versions may need polyfills for named arguments or attributes. Ensure your PHP installation includes the `stream_socket` extension (enabled by default) and `openssl` for TLS support. Check your Laravel version compatibility by reviewing the package’s changelog or testing in a staging environment.
Can I use this package for WebSocket connections in Laravel, or should I use something else?
This package supports raw TCP/TLS sockets, including WebSocket handshakes, but it lacks built-in WebSocket framing (e.g., masking, ping/pong). For full WebSocket support in Laravel, consider pairing it with a library like `ratchetphp/ws-server` or using Laravel-specific packages like `beberlei/laravel-websockets`. Use this package only if you need low-level socket control for WebSocket-like protocols.
How do I handle TLS/SSL certificates with custom validation or mutual TLS (mTLS) in Laravel?
Configure TLS settings via the socket client’s connection options, such as `context` or `ssl` parameters. For custom certificate validation, pass a `stream_context_create` array with `ssl` options (e.g., `verify_peer`, `cafile`, or `local_cert`). For mTLS, include `local_cert` and `local_pk` in the context. Laravel’s `config/services.php` can centralize these settings for reuse across socket requests.
What’s the best way to test socket-based functionality in Laravel’s CI/CD pipeline?
Mock network conditions using libraries like `Mockery` or `ReactPHP` for async testing. Avoid flaky tests by using in-memory sockets (e.g., `unix:///tmp/test.sock`) or local HTTP servers (e.g., `php -S localhost:8000`). Include a test setup script to generate SSL certificates (as shown in the package’s README) and validate socket timeouts, retries, and error handling in isolated test cases.
Is php-http/socket-client a good alternative to Guzzle or Laravel’s HttpClient for standard REST APIs?
No, this package is **not** a drop-in replacement for Guzzle or Laravel’s `HttpClient` for standard REST APIs. It’s designed for low-level socket operations where you need direct TCP/IP control, such as custom protocols, legacy systems, or high-performance networking. For REST APIs, use Laravel’s `HttpClient` (built on Guzzle) for its built-in resilience, middleware, and ease of use.
How do I implement connection pooling or retry logic with this socket client in Laravel?
This package doesn’t include built-in connection pooling or retries, so you’ll need to implement them manually. Use middleware in Laravel to wrap socket requests with retry logic (e.g., `php-http/retry`) or manage connection pools via a custom service. For async retries, integrate with Laravel’s queue system or use ReactPHP for event-driven retries. Document your retry strategy in configuration files for maintainability.
Can I use this package alongside Laravel’s HttpClient for hybrid use cases (e.g., fallback to sockets for legacy systems)?
Yes, you can combine both clients. For example, use Laravel’s `HttpClient` as the primary client and fall back to the socket client for legacy TCP-based APIs. Implement a service wrapper to abstract the decision logic (e.g., check API endpoints or response codes) and log fallback events. This approach ensures backward compatibility while leveraging modern Laravel features for new APIs.
What are the performance implications of using sockets vs. Guzzle in Laravel, and how can I benchmark them?
Socket-based clients like this package offer lower-level control but may introduce higher latency due to manual handling of TCP/IP layers. Benchmark using tools like `AB` (ApacheBench) or `k6` to compare throughput, latency, and resource usage. Test under realistic load (e.g., concurrent requests) and account for factors like connection reuse, TLS overhead, and protocol parsing. Document results to justify the trade-offs for your use case.
How do I monitor socket client performance and errors in a Laravel production environment?
Instrument the socket client with Laravel’s logging (e.g., `Log::debug`) or monitoring tools like Prometheus. Track metrics such as connection latency, error rates, and retry counts. Use Laravel’s `events` system to emit custom events for socket operations (e.g., `SocketConnected`, `SocketFailed`) and listen to them with services like Laravel Horizon or third-party monitoring tools. For TLS issues, log certificate validation failures separately.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity