- How do I install spatie/simple-tcp-client in a Laravel project?
- Run `composer require spatie/simple-tcp-client` in your project directory. The package has no Laravel-specific dependencies and works in any PHP 8.1+ environment, including Laravel 9+. Ensure the `ext-sockets` PHP extension is enabled for TCP functionality.
- Can this package handle TLS/SSL-encrypted TCP connections?
- No, this package only supports raw TCP connections. For TLS/SSL, you’ll need to use PHP’s `stream_socket_client` with SSL context or a dedicated library like `phpseclib`. The package is designed for unencrypted, protocol-agnostic TCP interactions.
- How do I manage connection timeouts or retries in spatie/simple-tcp-client?
- The package doesn’t include built-in retry logic, but you can wrap the `TcpClient` in middleware or a decorator class. For example, implement exponential backoff in a custom class that extends or uses `TcpClient` methods. Laravel’s service container makes dependency injection easy for such wrappers.
- Is spatie/simple-tcp-client suitable for high-frequency IoT device polling?
- Yes, but consider offloading TCP operations to Laravel queues or async workers to avoid blocking the request lifecycle. The package is lightweight and synchronous by default, so long-lived connections may require custom connection pooling or a separate microservice for scalability.
- Does this package support framing (e.g., length-prefixed messages) or multi-message exchanges?
- No, the package handles raw TCP streams without built-in framing logic. You’ll need to implement custom logic (e.g., parsing headers or delimiters) in your application code. For complex protocols, consider middleware or a higher-level wrapper around `TcpClient`.
- How can I integrate logging for TCP traffic in Laravel?
- The package doesn’t include built-in logging, but you can wrap `send()` and `receive()` methods with Laravel’s Monolog or a custom logger. For example, log the payload and timestamps before/after calling these methods, or use a decorator to inject logging behavior.
- Will spatie/simple-tcp-client work with Laravel’s service container?
- Yes, the package is designed to integrate seamlessly with Laravel’s service container. Bind the `TcpClient` to the container in `AppServiceProvider` or use dependency injection directly in controllers or services. This simplifies testing and reusability.
- Are there alternatives to spatie/simple-tcp-client for async TCP in Laravel?
- For asynchronous or non-blocking TCP, consider ReactPHP’s `react/socket` or `react/tcp`. These are more complex but better suited for event-driven architectures. `spatie/simple-tcp-client` is ideal for synchronous, lightweight TCP needs where async isn’t required.
- How do I test TCP interactions with this package in PHPUnit?
- Use PHPUnit’s mocking capabilities or Laravel’s `MockFacade` to simulate TCP responses. For example, mock the `TcpClient` class and stub `connect()`, `send()`, and `receive()` methods to return predefined data. Avoid real network calls in unit tests.
- Can I use this package for WebSocket-like long-lived TCP connections in Laravel?
- Technically yes, but long-lived connections may block Laravel’s request lifecycle. For production use, consider running TCP clients in a separate process (e.g., a Laravel queue worker or microservice) or using ReactPHP for async support. Monitor memory leaks if reusing connections.