Skip to content

Bindings & Events

evaluate() lets PHP ask the page for something. Bindings go the other way: the page calls a global JavaScript function, and your PHP callback receives the data. Together with pump(), this is how you build long-running bridges such as chat clients, monitors, and scrapers that react to the page instead of polling it.

exposeBinding() installs a global function in the page. When JavaScript calls it with a JSON string, PAM Browser decodes the JSON and calls your PHP callback:

binding.php
<?php
declare(strict_types=1);
require __DIR__.'/vendor/autoload.php';
use Pam\Browser\Browser;
$browser = Browser::launch();
try {
$page = $browser->newPage('data:text/html,<button id="buy">Buy</button>');
$page->exposeBinding('notifyPhp', function (mixed $payload): void {
printf("Page says: %s\n", json_encode($payload));
});
$page->evaluate(<<<'JS'
notifyPhp(JSON.stringify({ event: 'ready', items: [1, 2] }));
JS);
$page->pump(1.0); // deliver the binding call if it has not arrived yet
} finally {
$browser->close();
}
Page says: {"event":"ready","items":[1,2]}

The rules for bindings:

  • The name must be a valid JavaScript identifier (letters, digits, _ and $, not starting with a digit). Otherwise exposeBinding() throws ProtocolException.
  • The page must pass one string containing JSON. Anything else makes PAM Browser throw ProtocolException while processing the event.
  • The binding is registered with CDP Runtime.addBinding, so it is available in every document the page loads afterwards, not only the current one.

PAM Browser processes CDP messages whenever it is waiting for something: during navigate(), evaluate(), waitForFunction(), and other commands. Binding callbacks and response captures run at those moments.

When your code is idle, call pump() to process incoming messages yourself. It waits up to the given number of seconds for one message, dispatches it, and returns true, or returns false if nothing arrived:

watch.php
$page->exposeBinding('notifyPhp', function (mixed $payload): void {
// Store, forward, or react to the event.
});
$page->addScriptOnNewDocument(<<<'JS'
new MutationObserver(() => {
notifyPhp(JSON.stringify({ rows: document.querySelectorAll('.row').length }));
}).observe(document, { childList: true, subtree: true });
JS);
$page->navigate('https://example.com/dashboard');
while (true) {
$page->pump(1.0);
}

Because the loop blocks in pump() while there is nothing to do, an idle page costs almost no CPU.

CDP events are kept in memory only until they are consumed. PAM Browser keeps at most 256 queued events per event name and 256 out-of-order command responses; older entries are dropped first. This keeps a long-lived process from growing without bound when a page is chatty.

Waiting inside PAM Browser (endpoint discovery and waitForFunction() polling) uses Pam\Async\delay() when the PAM runtime provides it, and falls back to usleep() otherwise. The WebSocket transport is built on PAM’s native streams.

Each Browser owns one Chrome process. Reuse a single browser and open several pages rather than launching a browser per job; a page is much cheaper than a process.