【问题标题】:Using Laravel Echo (Pusher) on Laravel Vapor在 Laravel Vapor 上使用 Laravel Echo (Pusher)
【发布时间】:2020-04-06 15:04:27
【问题描述】:

我很难找出为什么我的事件没有在前端(Laravel Echo + Pusher)中“触发”,而我的应用程序是通过 Laravel Vapor 部署的,即使它在本地运行良好。

Pushers debug console 实际上表明事件实际上是由 Laravel 应用程序分派到 Pusher 的。

由于我几乎浪费了大约半天的时间来找出问题所在(幸运的是,我看到我的消息在我的本地环境中实时显示,而不是在登台发布内容后进行登台),我想我会再花 10 分钟在这里写一篇文章,这样一些人(希望)就不需要浪费太多时间了。

【问题讨论】:

    标签: laravel laravel-echo pusher-js laravel-vapor


    【解决方案1】:

    我已经花了 小时 试图找出同样的问题,但即将失去它。

    我刚刚发现这篇文章描述了一个有助于解决此问题的 npm 包。

    来源 https://codinglabs.com.au/blog/2019/8/using-environment-specific-variables-in-laravel-vapor-with-mix

    NPM 包 https://laravel-mix.com/extensions/env-file

    【讨论】:

    • 谢谢!似乎是一个更清洁的解决方案。我会对此进行调查,并在调查后将您的答案标记为已接受。
    【解决方案2】:

    问题实际上如下:

    • Vapor 首先在本地的 .vapor 目录中构建应用程序。
    • 在此构建过程中加载的来自 .env 的 PUSHER 键是您本地的 .env (!),这意味着如果您设置 MIX_PUSHER_APP_KEYMIX_PUSHER_APP_CLUSTER 环境变量,它不会改变任何内容在您的 .env.{environment} 中(您通过运行 vapor env:pull {environment} 获得。

    我以一种快速(且肮脏)的方式解决了这个问题:

    • 再添加几个环境变量:
    PUSHER_APP_ID=
    PUSHER_APP_KEY=
    PUSHER_APP_SECRET=
    PUSHER_APP_CLUSTER=eu
    
    # Necessary in `php artisan pusher:credentials`
    PUSHER_LOCAL_APP_KEY=
    PUSHER_LOCAL_APP_CLUSTER=eu
    
    # Necessary in `php artisan pusher:credentials`
    PUSHER_STAGING_APP_KEY=
    PUSHER_STAGING_APP_CLUSTER=eu
    
    # Necessary in `php artisan pusher:credentials`
    PUSHER_PRODUCTION_APP_KEY=
    PUSHER_PRODUCTION_APP_CLUSTER=eu
    
    MIX_PUSHER_APP_KEY="${PUSHER_LOCAL_APP_KEY}"
    MIX_PUSHER_APP_CLUSTER="${PUSHER_LOCAL_APP_CLUSTER}"
    
    • 在 Vapor 构建过程中添加一个新命令(yarn run production 之前):php artisan pusher:credentials {environment}。因此,对于分期,您可以使用 php artisan pusher:credentials staging

    命令如下所示:

    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    use Illuminate\Console\ConfirmableTrait;
    use Illuminate\Support\Str;
    
    final class SwapPusherCredentials extends Command
    {
        use ConfirmableTrait;
    
        private array $keyPatterns = [
            'PUSHER_%sAPP_KEY',
            'PUSHER_%sAPP_CLUSTER',
        ];
    
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'pusher:credentials {environment=production}';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Sets the pusher credentials based on the current environment';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @throws \Exception
         */
        public function handle()
        {
            // Basically we're fixing a bug (?) in Vapor. Since we're building the front end locally,
            // we need to swap the pusher keys before building. If we don't do this, the front end
            // will be built with local pusher keys. When posting messages not local it's broken
            $environment = Str::upper($this->argument('environment'));
    
            $this->updatePusherEnvironmentVariables($environment);
        }
    
        /**
         * @param string $environment
         * @throws \Exception
         */
        private function updatePusherEnvironmentVariables(string $environment)
        {
            foreach ($this->keyPatterns as $pattern) {
                // 'PUSHER_LOCAL_APP_KEY' || 'PUSHER_STAGING_APP_KEY' etc.
                $variableToSet = sprintf($pattern, $environment . '_');
    
                // 'PUSHER_APP_KEY'
                $targetVariableName = sprintf($pattern, '');
    
                if (!env($targetVariableName, false)) {
                    throw new \Exception('Missing environment value for ' . $targetVariableName);
                }
    
                $this->setVariable($targetVariableName, $variableToSet);
            }
        }
    
        private function setVariable(string $targetVariableName, string $variableToSet)
        {
            file_put_contents($this->laravel->environmentFilePath(), preg_replace(
                $this->replacementPattern($targetVariableName),
                $replacement = '${' . $variableToSet . '}',
                file_get_contents($this->laravel->environmentFilePath())
            ));
    
            $this->info("Successfully set MIX_{$targetVariableName} to {$replacement}!");
        }
    
        private function replacementPattern(string $targetVariableName)
        {
            // Don't remove notsurebutfixes, when removed it doesn't match the first entry
            // So it will match all keys except PUSHER_LOCAL_* for some reason.
            $escaped = '\=notsurebutfixes|\$\{' . $this->insertEnvironmentIntoKey('LOCAL',
                    $targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('STAGING',
                    $targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('PRODUCTION',
                    $targetVariableName) . '\}';
    
            return "/^MIX_$targetVariableName{$escaped}/m";
        }
    
        // Insert the environment after PUSHER_ in any PUSHER_* like variable passed in.
        // So basically PUSHER_APP_KEY => PUSHER_LOCAL_APP_KEY
        private function insertEnvironmentIntoKey($environment, $key)
        {
            $parts = explode('_', $key, 2);
    
            return $parts[0] . '_' . $environment . '_' . $parts[1];
        }
    }
    

    就是这样。现在.vapor 目录中的.env 文件将在部署期间更新为使用PUSHER_STAGING_APP_KEYPUSHER_STAGING_APP_CLUSTER

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2019-06-04
      • 2021-07-02
      • 2021-03-12
      相关资源
      最近更新 更多