Apr 18, 2025
0
Level 1
using wire:stream to stream output real time in livewire but not working idk why?
my livewire component
public $output = '';
public function runScript()
{
set_time_limit(0);
$this->output = '';
try {
$script = $this->getModifiedScript();
$script2 = $this->getSecondScript();
$this->appendOutput("Executing script on server {$this->server->ip_address}...\n");
$this->appendOutput("Connected successfully via SFTP!\n");
$this->appendOutput("Uploading setup script...\n");
$sftp = $this->connectToSftp();
$scriptPath1 = '/tmp/vpn_setup_' . time() . '.sh';
if (!$sftp->put($scriptPath1, $script)) {
throw new \Exception("Failed to upload vpn script to the server.");
}
$this->appendOutput("Script uploaded successfully!\n");
$scriptPath2 = '/tmp/vpn_setup_api_' . time() . '.sh';
if (!$sftp->put($scriptPath2, $script2)) {
throw new \Exception("Failed to upload vpn api script to the server.");
}
$this->appendOutput("Second script uploaded successfully!\n");
$sftp->disconnect();
$this->appendOutput("SFTP disconnected\n");
$this->appendOutput("Connecting to server via SSH...\n");
$ssh = $this->connectToServer();
if (!$ssh) {
throw new \Exception("SSH connection failed.");
}
$this->appendOutput("Connected successfully via SSH!\n");
$ssh->setTimeout(600); // 10 minutes timeout
$this->appendOutput("Setting up VPN...\n");
$this->appendOutput("Making scripts executable...\n");
$this->appendOutput("Changing permissions for both scripts...\n");
$ssh->exec("chmod +x {$scriptPath1}");
$ssh->exec("chmod +x {$scriptPath2}");
$this->appendOutput("Scripts made executable!\n");
$this->appendOutput("\n=== Starting VPN Setup Script ===\n\n");
$this->appendOutput($ssh->exec("bash {$scriptPath1}"));
$this->appendOutput("\n=== VPN Setup Script Completed ===\n\n");
$this->appendOutput("\n=== Starting VPN API Setup Script ===\n\n");
$this->appendOutput($ssh->exec("bash {$scriptPath2}"));
$this->appendOutput("\n=== VPN API Setup Script Completed ===\n\n");
$this->appendOutput("VPN setup completed successfully!\n");
$this->appendOutput("Cleaning up both files...\n");
$ssh->exec("rm -f {$scriptPath1} {$scriptPath2}");
$this->appendOutput("Temporary scripts removed from server.\n");
$this->appendOutput("Disconnecting from server...\n");
$ssh->disconnect();
$this->appendOutput("Disconnected successfully!\n");
$this->appendOutput("Script execution completed!\n");
} catch (\Exception $e) {
$this->appendOutput("ERROR: " . $e->getMessage());
$this->dispatch('sweetToast', type: 'error', message: $e->getMessage(), title: 'Error!');
Log::channel('ssh')->error("Error executing script on {$this->server->ip_address}:", [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
}
private function appendOutput($text)
{
$this->stream(to: 'output', content: $text);
}
blade view
<div class="row mb-3">
<div class="col-md-12">
<div class="card bg-dark text-white">
<div class="card-header">
Script Output
</div>
<div class="card-body" wire:stream="output">
<pre id="script-output" style="height: 300px; overflow-y: auto; white-space: pre-wrap;">
{{ $output }}
</pre>
</div>
</div>
</div>
</div>
how could i stream this output in real time
$this->appendOutput("Executing script on server {$this->server->ip_address}...\n");
$this->appendOutput("Connected successfully via SFTP!\n");
$this->appendOutput("Uploading setup script...\n");
private function appendOutput($text)
{
$this->stream(to: 'output', content: $text);
}
any help would be appreciated.
Please or to participate in this conversation.