Installation
Run composer require colemando/ratchetio-bundle in your Laravel project (note: this bundle is Symfony2-focused, but can be adapted for Laravel via bridge packages like symfony/bundle or manual integration).
Service Provider & Config
config/services.php:
'ratchetio' => [
'access_token' => env('RATCHETIO_ACCESS_TOKEN'),
],
config/app.php:
'providers' => [
// ...
Ratchetio\ServiceProvider::class,
],
First Use Case: Logging Exceptions
Wrap your application’s exception handler (e.g., app/Exceptions/Handler.php) to forward errors to Ratchet.io:
use Ratchetio\Ratchetio;
public function report(Throwable $exception)
{
Ratchetio::send($exception);
parent::report($exception);
}
Exception Reporting
App\Exceptions\Handler to auto-send exceptions to Ratchet.io.Ratchetio::send(new \RuntimeException("Custom error message"));
Configuration Management
.env or cache them:
config(['ratchetio.access_token' => env('RATCHETIO_TOKEN')]);
config/ratchetio.php).Integration with Monolog
use Ratchetio\Handler\RatchetioHandler;
$logger->pushHandler(new RatchetioHandler(config('ratchetio.access_token')));
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
Ratchetio::send($e);
throw $e;
}
}
Ratchetio::queue(new \RuntimeException("Deferred error"));
Token Security
.env or Laravel’s env() helper.if (!config('ratchetio.access_token')) {
throw new \RuntimeException("Ratchet.io token not configured");
}
Performance Overhead
Symfony2 Legacy
HttpFoundation; wrap exceptions in Laravel’s HttpException for compatibility:
Ratchetio::send(new \Symfony\Component\HttpKernel\Exception\HttpException(500, "Error"));
Ratchetio::setLogger(new \Monolog\Logger('test', [new \Monolog\Handler\StreamHandler(storage_path('logs/ratchetio.log'))]));
200 for successful submissions).error_class, message).Custom Payloads Override the default payload structure:
Ratchetio::setPayloadTransformer(function ($exception) {
return [
'error_class' => get_class($exception),
'message' => $exception->getMessage(),
'extra' => ['user_id' => auth()->id()],
];
});
Webhook Integration Extend the bundle to trigger webhooks on critical errors:
Ratchetio::onSend(function ($payload) {
Http::post('https://your-webhook.com', $payload);
});
Testing Mock Ratchet.io in tests:
Ratchetio::setClient(new class {
public function send($payload) {
// Assert payload or store for testing
}
});
How can I help you explore Laravel packages today?