You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Echo本地正常运行,部署到生产环境后无响应

问题描述

通知功能本地运行正常,但部署到生产环境后,控制台无任何输出。相关代码如下:

@pushonce('custom-scripts')
    <script>
        Echo.private('App.Models.User.' + @js($user->id)).notification((notification) => {
            console.log(notification); <---生产环境控制台无打印内容。
            @this.call('addNotification', notification);
        });
    </script>
@endpushonce

已确认信息:

  • 生产环境控制台输入Echo可正常识别,Echo已正确引入
  • 生产环境执行过以下命令,无效果:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan optimize:clear
  • 使用GitHub Actions作为CI/CD流水线,构建脚本如下:
- uses: actions/checkout@main
      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Install Dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
      - name: Generate key
        run: php artisan key:generate
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Install dependencies
        run: npm install
      - name: Compile assets
        run: npm run production
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        run: vendor/bin/phpunit
  • package.json的scripts配置:
"scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
  • 本地执行npm run prod/npm run production生成的产物与生产环境一致,本地可正常打印通知,排除构建脚本问题;Echo已包含在生产构建中。
排查与解决方案

1. 检查生产环境Broadcaster配置

确认.env中的广播驱动配置正确:

  • 若用Pusher:确保BROADCAST_DRIVER=pusher,且PUSHER_APP_ID、PUSHER_APP_KEY、PUSHER_APP_SECRET、PUSHER_APP_CLUSTER与生产环境Pusher控制台一致,APP_URL为生产环境正确域名。
  • 若用Redis:确认Redis服务在生产环境正常运行,BROADCAST_DRIVER=redis,Redis连接配置无误。

2. 验证频道授权是否正常

生产环境下,登录后访问/broadcasting/auth接口,检查是否返回200状态码。若返回403或其他错误,排查:

  • routes/channels.php中private频道的授权规则是否正确,示例:
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
  • 生产环境会话配置是否正确,确保用户登录状态能在广播授权请求中正常传递(比如会话驱动、Cookie域名匹配生产域名)。

3. 检查WebSocket连接状态

在生产环境浏览器控制台执行以下命令,查看Echo连接状态:

Echo.connector.pusher.connection.state

若状态不是connected,说明WebSocket未建立:

  • 若用Pusher,检查Laravel Forge防火墙是否放行WebSocket相关端口(443、80、6001等);确认PUSHER_APP_ENCRYPTED=true时,前端使用wss协议连接。

4. 检查通知事件发送逻辑

生产环境触发通知后,查看Laravel日志(storage/logs/laravel.log),确认事件是否成功发送到广播驱动:

  • 若用Pusher,可直接登录Pusher控制台的「Debug Console」查看是否有事件推送。
  • 确保通知类正确实现ShouldBroadcast接口,指定正确频道,示例:
class ExampleNotification extends Notification implements ShouldBroadcast
{
    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }

    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'data' => '你的通知内容'
        ]);
    }
}

5. 验证前端频道订阅是否正确

在生产环境控制台打印用户ID,确认频道名称与后端完全一致:

console.log('App.Models.User.' + 123); // 替换为当前用户实际ID值

注意命名空间大小写、格式是否和后端通知发送的频道匹配。

6. 检查生产环境CORS配置

若前后端域名不同,确认config/cors.php配置允许WebSocket请求:

'paths' => ['api/*', 'broadcasting/auth'],
'allowed_methods' => ['*'],
'allowed_origins' => ['生产环境前端域名'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,

确保supports_credentials设为true,保证会话Cookie能在跨域请求中传递。

内容的提问来源于stack exchange,提问作者hyphen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 22:45:58