
Reverb 是 Laravel 中用于實時事件廣播的 Pusher 的實用替代方案。本指南重點介紹在 Laravel 11 中為托管在 Cloudflare 后面且具有靈活 SSL 的實時制作系統(tǒng)配置 Reverb。
先決條件
在深入設(shè)置之前,請確保您具備以下條件:
-
已安裝 Laravel 11:您可以使用 Composer 設(shè)置新的 Laravel 11 應(yīng)用程序。
-
Apache Web 服務(wù)器:確保 Apache 已安裝并正在運行。
-
Cloudflare 帳戶:您的應(yīng)用程序應(yīng)設(shè)置在 Cloudflare 后面并啟用靈活的 SSL。
安裝混響
首先,您需要在 Laravel 項目中安裝 Reverb。運行以下 Composer 命令:
composer require laravel/reverb
安裝完成后,發(fā)布配置文件:
php artisan vendor:publish --provider="Laravel\Reverb\ReverbServiceProvider"
這將創(chuàng)建一個 config/reverb.php 文件,您可以在其中調(diào)整混響的設(shè)置。
混響配置示例
以下是混響的配置示例:
<?php
return [
'default' => env('REVERB_SERVER', 'reverb'),
'servers' => [
'reverb' => [
'host' => env('REVERB_HOST', '0.0.0.0'),
'port' => env('REVERB_PORT', 6001),
'hostname' => env('REVERB_HOST'),
'options' => [
'tls' => [],
],
'max_request_size' => env('REVERB_MAX_REQUEST_SIZE', 10_000),
'scaling' => [
'enabled' => env('REVERB_SCALING_ENABLED', false),
'channel' => env('REVERB_SCALING_CHANNEL', 'reverb'),
'server' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', '6379'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'database' => env('REDIS_DB', '0'),
],
],
'pulse_ingest_interval' => env('REVERB_PULSE_INGEST_INTERVAL', 15),
'telescope_ingest_interval' => env('REVERB_TELESCOPE_INGEST_INTERVAL', 15),
],
],
'apps' => [
'provider' => 'config',
'apps' => [
[
'key' => env('REVERB_APP_KEY'),
'secret' => env('REVERB_APP_SECRET'),
'app_id' => env('REVERB_APP_ID'),
'options' => [
'host' => env('REVERB_HOST'),
'port' => env('REVERB_PORT', 443),
'scheme' => env('REVERB_SCHEME', 'https'),
'useTLS' => env('REVERB_SCHEME', 'https') === 'https',
],
'allowed_origins' => ['*'],
'ping_interval' => env('REVERB_APP_PING_INTERVAL', 60),
'activity_timeout' => env('REVERB_APP_ACTIVITY_TIMEOUT', 30),
'max_message_size' => env('REVERB_APP_MAX_MESSAGE_SIZE', 10_000),
],
],
],
];
.env 設(shè)置
確保在您的 .env 文件中正確配置以下環(huán)境變量:
BROADCAST_CONNECTION=reverb
QUEUE_CONNECTION=database
REVERB_HOST=127.0.0.1
REVERB_PORT=6001
REVERB_APP_ID=<app-key>
REVERB_APP_KEY=<app-key>
REVERB_APP_SECRET=<app-secret>
REVERB_SCHEME=http
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="example.com"
VITE_REVERB_PORT=443
VITE_REVERB_SCHEME=https
創(chuàng)建事件
使用以下 Artisan 命令生成新的事件類:
php artisan make:event MessageSent
以下是 MessageSent 事件的示例實現(xiàn):
<?php
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\Channel;
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn(): Channel
{
return new Channel('chat-channel');
}
public function broadcastAs(): string
{
return 'message-sent';
}
}
Laravel 刀片示例
創(chuàng)建一個簡單的 Blade 模板來測試混響功能 (welcome.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Laravel Reverb WebSocket Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pusher/8.3.0/pusher.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/laravel-echo/1.17.1/echo.iife.min.js"></script>
</head>
<body>
<h1>Laravel Reverb WebSocket Test</h1>
<p>Open the console to see WebSocket messages.</p>
<button>
<h2>
Defining Routes
</h2>
<p>Below is the code to define the required routes:<br>
</p>
<pre class="brush:php;toolbar:false"><?php
use Illuminate\Support\Facades\Route;
use App\Events\MessageSent;
Route::post('/send-message', function (\Illuminate\Http\Request $request) {
event(new MessageSent($request->input('message')));
return response()->json(['success' => true]);
});
Route::get('/', function () { return view('welcome'); });
阿帕奇配置
運行以下命令以啟用必要的 Apache 模塊:
sudo a2enmod proxy
sudo a2enmod proxy_wstunnel
sudo a2enmod rewrite
下面是 Apache VirtualHost 設(shè)置的示例配置:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
DocumentRoot /var/www/example.com/public
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /app ws://127.0.0.1:6001/app
ProxyPassReverse /app ws://127.0.0.1:6001/app
SetEnvIf X-Forwarded-Proto https HTTPS=on
ErrorLog /var/www/logs/example.com_error.log
CustomLog /var/www/logs/example.com_access.log combined
</VirtualHost>
<Directory /var/www/example.com>
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
運行服務(wù)
要啟動服務(wù),您需要啟動事件工作線程和 Rebel 服務(wù)器。
運行以下命令來啟動事件工作線程:
php artisan queue:work
使用以下命令在指定端口和主機上啟動 Rebel 服務(wù)器:
php artisan reverb:start --port=6001 --host=0.0.0.0
結(jié)論
如果您不使用 Laravel Echo 和 Pusher 的 CDN,則需要安裝所需的 npm 庫(pusher-js 和 laravel-echo)以將實時事件廣播集成到您的應(yīng)用程序中。此設(shè)置需要前端構(gòu)建過程來管理和捆綁項目中的庫。
對于使用完整 SSL 托管在 Cloudflare 后面的應(yīng)用程序,必須使用正確定義的 SSL 證書配置單獨的 VirtualHost。這可確保安全的 WebSocket 通信并避免 SSL/TLS 不匹配的問題,從而阻止 WebSocket 連接正常運行。
以上是使用 Apache 在 Laravel 中配置混響的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!