Laravel Horizon is a powerful tool for monitoring and managing queues in Laravel applications. If you want to integrate Laravel Horizon with ReactJS for a more interactive and dynamic queue management experience, you can follow these steps:
Ensure you have Laravel installed and then install Horizon:
bashcomposer require laravel/horizon
Publish the Horizon assets and configuration:
bashphp artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
Configure your Horizon dashboard in the horizon.php
configuration file. You can find this file in the config
directory.
php// config/horizon.php
return [
// ...
'middleware' => ['web', 'auth'],
'path' => 'horizon',
'domain' => env('HORIZON_DOMAIN', null),
// ...
];
Make sure to add the 'auth' middleware to secure the Horizon dashboard.
Laravel Horizon uses Laravel Echo and Pusher for real-time updates. Install them:
bashcomposer require pusher/pusher-php-server npm install --save laravel-echo pusher-js
Configure broadcasting in your Laravel application. Update your .env
file with the necessary broadcasting credentials.
envBROADCAST_DRIVER=pusher PUSHER_APP_ID=your-app-id PUSHER_APP_KEY=your-app-key PUSHER_APP_SECRET=your-app-secret PUSHER_APP_CLUSTER=your-app-cluster
Set up your ReactJS environment. You can use Create React App or any other React setup.
Create a React component for the Horizon dashboard. You can use Laravel Echo and Pusher to listen for updates and reflect them in your React component.
jsx// resources/js/components/HorizonDashboard.js
import React, { useState, useEffect } from 'react';
import Echo from 'laravel-echo';
const HorizonDashboard = () => {
const [metrics, setMetrics] = useState([]);
useEffect(() => {
const echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: true,
});
echo.channel('horizon')
.listen('HorizonMetricsUpdated', (data) => {
setMetrics(data.metrics);
});
}, []);
return (
<div>
<h1>Horizon Dashboard</h1>
<ul>
{metrics.map(metric => (
<li key={metric.name}>{metric.name}: {metric.value}</li>
))}
</ul>
</div>
);
};
export default HorizonDashboard;
Include the React component in your Blade view:
blade<!-- resources/views/horizon.blade.php --> @extends('layouts.app') @section('content') <div id="horizon-dashboard"></div> <script src="{{ mix('js/app.js') }}"></script> @endsection
Run your Laravel application and the React development server:
bashphp artisan serve npm run watch
Visit http://localhost:8000/horizon
to see your Horizon dashboard with real-time updates.
This is a basic example, and you can customize and expand it based on your application's requirements. Make sure to refer to the Laravel Horizon documentation for more advanced configurations and features.