Skip to content

Commit

Permalink
[11.x] Allow install:broadcasting to populate echo.js configuration
Browse files Browse the repository at this point in the history
either with or without Reverb

fixes #50440

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Mar 11, 2024
1 parent 20d2d1e commit 4968875
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\PhpExecutableFinder;

use function Illuminate\Filesystem\join_paths;
use function Laravel\Prompts\confirm;

#[AsCommand(name: 'install:broadcasting')]
Expand Down Expand Up @@ -53,9 +54,11 @@ public function handle()
$this->uncommentChannelsRoutesFile();
}

$installReverb = $this->shouldInstallReverb();

// Install bootstrapping...
if (! file_exists($echoScriptPath = $this->laravel->resourcePath('js/echo.js'))) {
copy(__DIR__.'/stubs/echo-js.stub', $echoScriptPath);
copy(join_paths(__DIR__, 'stubs', $installReverb ? 'echo-js.stub' : 'echo.js.pusher.stub'), $echoScriptPath);
}

if (file_exists($bootstrapScriptPath = $this->laravel->resourcePath('js/bootstrap.js'))) {
Expand All @@ -71,7 +74,9 @@ public function handle()
}
}

$this->installReverb();
if ($installReverb) {
$this->performReverbInstallation();
}

$this->installNodeDependencies();
}
Expand Down Expand Up @@ -111,18 +116,12 @@ protected function uncommentChannelsRoutesFile()
*
* @return void
*/
protected function installReverb()
protected function performReverbInstallation()
{
if (InstalledVersions::isInstalled('laravel/reverb')) {
return;
}

$install = confirm('Would you like to install Laravel Reverb?', default: true);

if (! $install) {
return;
}

$this->requireComposerPackages($this->option('composer'), [
'laravel/reverb:@beta',
]);
Expand All @@ -138,6 +137,20 @@ protected function installReverb()
$this->components->info('Reverb installed successfully.');
}

/**
* Determine if Reverb should be installed.
*
* @return bool
*/
public function shouldInstallReverb()
{
if (InstalledVersions::isInstalled('laravel/reverb')) {
return true;
}

return confirm('Would you like to install Laravel Reverb?', default: true);
}

/**
* Install and build Node dependencies.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/echo-js.pusher.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Echo from 'laravel-echo';

import Pusher from 'pusher-js';
window.Pusher = Pusher;

window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
wsHost: import.meta.env.VITE_PUSHER_HOST ? import.meta.env.VITE_PUSHER_HOST : `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});

0 comments on commit 4968875

Please sign in to comment.