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,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保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对性能的影响。

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版
中文版,非常好用

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中