- How do I install spatie/ping in a Laravel project?
- Run `composer require spatie/ping` in your project directory. The package has no Laravel-specific dependencies and works with PHP 8.1+. No additional configuration is needed for basic usage.
- Can I use this package to monitor external APIs or services?
- Yes, but ICMP may not be ideal for APIs behind firewalls. For HTTP/HTTPS endpoints, consider combining this with HTTP checks (e.g., Guzzle) or use a hybrid approach with fallback logic. ICMP is better suited for raw network diagnostics.
- How do I handle errors or retries if a ping fails?
- The package doesn’t include built-in retry logic. Use Laravel’s retry helper (e.g., `try-catch` with `retry()`) or wrap the ping in a Laravel Job with `spatie/queueable-model` for async retries. Circuit breakers must be implemented manually.
- Does spatie/ping work on Windows in production?
- Windows requires admin privileges for ICMP. For production, use Docker with a Linux container or restrict usage to Linux/macOS hosts. Test thoroughly in staging to avoid permission issues.
- How can I integrate ping results into Laravel monitoring tools?
- Use the structured `PingResult` object to log metrics (e.g., `packetLossPercentage()`, `averageTimeInMs()`) via `spatie/laravel-logging` for ELK or export to Prometheus with `spatie/laravel-prometheus`. For alerts, pipe results to `spatie/laravel-monitor`.
- What Laravel versions does spatie/ping support?
- The package supports PHP 8.1+ and works with Laravel 9+. For older Laravel versions (e.g., 8.x), ensure your PHP version is compatible. No Laravel-specific features are required.
- How do I test spatie/ping in a CI/CD pipeline?
- Unit testing ICMP is challenging due to system call dependencies. Use integration tests in staging/production or mock the `Ping` class in unit tests. Test edge cases like packet loss, high latency, and error responses.
- Is there a way to run multiple pings concurrently?
- No, the package runs pings sequentially. For concurrent checks, dispatch pings as Laravel Jobs with `spatie/queueable-model` or use a sidecar service (e.g., Redis Queue). Avoid aggressive pinging (>10/sec) to prevent network throttling.
- What alternatives exist if ICMP is blocked by firewalls?
- Fallback to TCP-based checks (e.g., HTTP HEAD requests with Guzzle) or DNS resolution (e.g., `dns_get_record()`). For hybrid monitoring, create a wrapper class that tries ICMP first, then falls back to HTTP if ICMP fails.
- How accurate are the latency measurements compared to native `ping` commands?
- The package mirrors system `ping` output but may have slight variations due to PHP process overhead. For critical latency monitoring, cross-validate with native tools (e.g., `ping -c 4`) or use TCP-based alternatives for sub-millisecond precision.