- How do I install spatie/ping in a Laravel project?
- Run `composer require spatie/ping` in your project root. The package has no Laravel-specific dependencies and works with PHP’s native `shell_exec()` or `proc_open()`. Test your environment after installation to ensure the system ping command is accessible.
- What Laravel versions does spatie/ping support?
- The package is framework-agnostic but works seamlessly with Laravel 8.x, 9.x, and 10.x. It leverages PHP 8.0+ features and has no Laravel-specific version constraints. Test with your target Laravel version to confirm compatibility.
- Can I use this for real-time latency monitoring in a Laravel app?
- Yes, but avoid blocking HTTP requests. Wrap `Ping::run()` in a Laravel job (e.g., `PingHealthCheckJob`) and dispatch it from a queue worker or cron job. For real-time decisions, cache results or use Laravel Echo to push updates.
- How do I handle cross-platform issues (e.g., macOS vs. Linux ping flags)?
- The package auto-detects OS-specific ping flags, but some systems (e.g., macOS) may require adjustments. Test on all target environments. Override defaults via `config('ping.custom_flags')` if needed, or extend the `Ping` class for custom logic.
- What’s the best way to store ping results for historical analysis?
- Store results in a Laravel migration table (e.g., `ping_results`) with columns for `host`, `timestamp`, `packet_loss`, `avg_rtt`, and `status`. Use Laravel Scout or a time-series DB like InfluxDB for large-scale data. Add a `PingResultObserver` to log results automatically.
- How do I trigger alerts when ping failures or high latency occur?
- Dispatch Laravel events (e.g., `PingFailed`, `HighLatency`) when thresholds are breached. Use `PingResult::packetLossPercentage() > 20` or `PingResult::averageTimeInMs() > 500`. Subscribe to events in `EventServiceProvider` and send notifications via Laravel Notifications or webhooks.
- Is spatie/ping suitable for high-volume ping checks (e.g., 100+ hosts)?
- No—system ping calls are blocking. Queue jobs for async execution and limit concurrent checks to avoid system overload. For large-scale monitoring, consider dedicated agents like Telegraf or specialized services (e.g., Pingdom).
- How do I mock spatie/ping for unit testing in Laravel?
- Use Laravel’s `Mockery` or `Pest` to stub `shell_exec()`. Example: `Mock::shouldReceive('exec')->once()->andReturn('PING output...');`. Test `PingResult` methods like `isSuccess()` or `packetLossPercentage()` against mocked responses.
- Can I integrate this with Laravel Horizon for scheduled ping checks?
- Yes. Create a `PingHealthCheckJob` and schedule it in `app/Console/Kernel.php` under `schedule()`. Use Horizon’s dashboard to monitor job progress and failures. Example: `$schedule->job(new PingHealthCheckJob)->everyMinute();`
- What are the alternatives to spatie/ping for Laravel network monitoring?
- For lightweight checks, consider `spatie/ping` or PHP’s `fsockopen()` for TCP checks. For production-grade monitoring, use dedicated tools like New Relic, Datadog, or Telegraf. If you need ICMP-specific features, `spatie/ping` is the most Laravel-friendly option.