Run a browser server
Run a long-lived browser server and print its wsEndpoint.
This example starts a long-lived Playwright browser server and prints the wsEndpoint that another process can use to connect.
Use it when you want one process to own the browser and another process to attach to it.
Run it
In terminal 1:
php content/examples/launch-server.php
Expected output:
Launching browser server...
Browser server running!
WebSocket endpoint: ws://127.0.0.1:...
Press Ctrl+C to stop the server...
Keep this process running. Copy the full ws://... endpoint, including its path.
To stop the server, press Ctrl+C in the terminal that runs this script.
Connect from another process
In terminal 2:
REMOTE_WS_ENDPOINT='ws://127.0.0.1:PORT/PATH' php content/examples/connect-remote-browser.php
Replace the value with the endpoint printed by the server.
What it demonstrates
launchServer()starts a browser server that can accept external clients.wsEndpoint()is the connection string for Playwright protocol clients.- The server stays alive until the PHP process exits.
Use Connect to a remote browser for the client side of the same workflow.
Go next
- Read the client example: Connect to a remote browser
- Understand browser connections: Connect browser
- Learn internals: Transport
Source
launch-server.php<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require __DIR__.'/../../vendor/autoload.php';
use Playwright\PlaywrightFactory;
// Start a reusable Playwright browser server for external clients.
// This is useful when you want one long-lived browser shared by many scripts.
$playwright = PlaywrightFactory::create();
echo "Launching browser server...\n";
$server = $playwright->launchServer('chromium', [
'headless' => true,
]);
$wsEndpoint = $server->wsEndpoint();
echo "Browser server running!\n";
echo "WebSocket endpoint: {$wsEndpoint}\n";
echo "\nOther processes can connect using:\n";
echo "\$browser = \$playwright->chromium()->connect('{$wsEndpoint}');\n";
echo "\nPress Ctrl+C to stop the server...\n";
// Keep serving until interrupted so clients can keep connecting to this endpoint.
while (true) {
sleep(1);
}