PHP code example of artemyurov / laravel-autossh-tunnel
1. Go to this page and download the library: Download artemyurov/laravel-autossh-tunnel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
artemyurov / laravel-autossh-tunnel example snippets
use ArtemYurov\Autossh\Facades\Tunnel;
// Access remote database
Tunnel::connection('remote_db')->execute(function() {
// Connect to remote PostgreSQL via localhost:15432
$users = DB::connection('pgsql_remote')->table('users')->get();
});
use ArtemYurov\Autossh\Facades\Tunnel;
// Expose local Laravel app for webhook testing
Tunnel::connection('local_webhooks')->execute(function() {
$this->info('Local app is now accessible at http://your-server.com:8080');
$this->info('Configure webhooks to point to this URL');
// Keep tunnel open while testing
sleep(3600); // 1 hour
});
use ArtemYurov\Autossh\Facades\Tunnel;
// Using configuration from config/tunnel.php
Tunnel::connection('remote_db')->execute(function() {
// Tunnel is active, you can work with remote service
// Tunnel will automatically close after execution
});
use ArtemYurov\Autossh\Facades\Tunnel;
use Illuminate\Support\Facades\DB;
Tunnel::connection('remote_db')
->withDatabaseConnection('pgsql_remote', [
'driver' => 'pgsql',
'database' => env('REMOTE_DB_DATABASE'),
'username' => env('REMOTE_DB_USERNAME'),
'password' => env('REMOTE_DB_PASSWORD'),
])
->execute(function() {
// Now you can use the connection
$users = DB::connection('pgsql_remote')->table('users')->get();
// Tunnel will automatically close after execution
});
use ArtemYurov\Autossh\Facades\Tunnel;
$connection = Tunnel::connection('remote_db')->start();
try {
// Work with tunnel
$pid = $connection->getPid();
$isRunning = $connection->isRunning();
} finally {
// Must close the tunnel
$connection->stop();
}
use ArtemYurov\Autossh\Tunnel;
// PostgreSQL tunnel
$pgTunnel = Tunnel::connection('pgsql')->start();
// MySQL tunnel
$mysqlTunnel = Tunnel::connection('mysql')->start();
try {
// Work with both tunnels
} finally {
$pgTunnel->stop();
$mysqlTunnel->stop();
}
namespace App\Console\Commands;
use Illuminate\Console\Command;
use ArtemYurov\Autossh\Facades\Tunnel;
use Illuminate\Support\Facades\DB;
class SyncDatabase extends Command
{
protected $signature = 'db:sync';
public function handle(): int
{
return Tunnel::connection('remote_db')
->withDatabaseConnection('remote_db', [
'driver' => 'pgsql',
'database' => env('REMOTE_DB_DATABASE'),
'username' => env('REMOTE_DB_USERNAME'),
'password' => env('REMOTE_DB_PASSWORD'),
])
->execute(function() {
$this->info('Syncing database...');
// Your sync logic
$tables = DB::connection('remote_db')
->select("SELECT tablename FROM pg_tables WHERE schemaname = 'public'");
$this->info('Found tables: ' . count($tables));
return Command::SUCCESS;
});
}
}
use ArtemYurov\Autossh\Tunnel;
// Automatically reuse existing tunnel or create new one
$tunnel = Tunnel::connection('remote_db')->reuseOrCreate();
// Or explicitly find existing tunnel by port
$pid = $tunnel->findExistingByPort();
if ($pid) {
echo "Found existing tunnel with PID: $pid";
}
use ArtemYurov\Autossh\Tunnel;
$tunnel = Tunnel::connection('remote_db')
->withDatabaseConnection('pgsql_remote', [...])
->start();
// Execute with automatic retry on connection loss
$result = $tunnel->executeWithRetry(function() {
return DB::connection('pgsql_remote')->table('users')->count();
}, $maxAttempts = 3);
'retry' => [
'max_attempts' => 3, // Maximum retry attempts
'delay' => 2, // Delay between retries (seconds)
'exponential' => false, // Use exponential backoff (2s, 4s, 8s...)
],
use ArtemYurov\Autossh\Tunnel;
$connection = Tunnel::connection('remote_db')
->withDatabaseConnection('pgsql_remote', [...])
->start();
// Simple validation (SELECT 1 query)
if ($connection->validateDatabase('pgsql_remote')) {
echo "Database is accessible";
}
// Wait for database with retries
if ($connection->waitForDatabase('pgsql_remote', $maxAttempts = 5, $delaySeconds = 2)) {
echo "Database became available";
}
// Full tunnel validation (process + port + database)
$result = $connection->validate('pgsql_remote');
if ($result['valid']) {
echo "Tunnel is fully operational";
} else {
foreach ($result['errors'] as $error) {
echo "Error: $error\n";
}
}
use ArtemYurov\Autossh\Tunnel;
$connection = Tunnel::connection('remote_db')
->start()
->setupSignalHandlers(); // Handle SIGINT, SIGTERM
// Tunnel will be properly closed when receiving Ctrl+C or kill signal