Reverb 是 Laravel 中用於即時事件廣播的 Pusher 的實用替代方案。本指南重點介紹在 Laravel 11 中為託管在 Cloudflare 後面且具有靈活 SSL 的即時製作系統配置 Reverb。
先決條件
在深入設定之前,請確保您具備以下條件:
- 已安裝 Laravel 11:您可以使用 Composer 設定新的 Laravel 11 應用程式。
- Apache Web 伺服器:確保 Apache 已安裝並正在運作。
- Cloudflare 帳戶:您的應用程式應設定在 Cloudflare 後面並啟用靈活的 SSL。
安裝混響
首先,您需要在 Laravel 專案中安裝 Reverb。執行以下 Composer 指令:
composer require laravel/reverb
安裝完成後,發布設定檔:
php artisan vendor:publish --provider="Laravel\Reverb\ReverbServiceProvider"
這將建立一個 config/reverb.php 文件,您可以在其中調整混響的設定。
混響配置範例
以下是混響的配置範例:
<?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 設定
確保在您的 .env 檔案中正確配置以下環境變數:
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 </app-secret></app-key></app-key>
建立事件
使用下列 Artisan 指令產生新的事件類別:
php artisan make:event MessageSent
以下是 MessageSent 事件的範例實作:
<?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 刀片範例
建立一個簡單的 Blade 模板來測試混響功能 (welcome.blade.php):
<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> <h1 id="Laravel-Reverb-WebSocket-Test">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 設定的範例設定:
<virtualhost> 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> Options -Indexes +FollowSymLinks -MultiViews AllowOverride All Require all granted </directory>
運行服務
要啟動服務,您需要啟動事件工作執行緒和 Rebel 伺服器。
執行以下命令來啟動事件工作執行緒:
php artisan queue:work
使用下列指令在指定連接埠和主機上啟動 Rebel 伺服器:
php artisan reverb:start --port=6001 --host=0.0.0.0
結論
如果您不使用 Laravel Echo 和 Pusher 的 CDN,則需要安裝所需的 npm 庫(pusher-js 和 laravel-echo)以將即時事件廣播整合到您的應用程式中。此設定需要前端建置流程來管理和捆綁專案中的庫。
對於使用完整 SSL 託管在 Cloudflare 後面的應用程序,必須使用正確定義的 SSL 憑證來配置單獨的 VirtualHost。這可確保安全的 WebSocket 通訊並避免 SSL/TLS 不匹配的問題,從而阻止 WebSocket 連線正常運作。
以上是使用 Apache 在 Laravel 中設定混響的詳細內容。更多資訊請關注PHP中文網其他相關文章!

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。