PHP code example of anandpilania / php-node-bridge
1. Go to this page and download the library: Download anandpilania/php-node-bridge 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/ */
anandpilania / php-node-bridge example snippets
PhpNodeBridge\Bridge\PhpBridge;
$bridge = new PhpBridge([
'port' => 5556,
'nodePort' => 5555,
'secret' => 'my-secret',
]);
// Register handlers Node.js can call
$bridge
->register('invoice.create', function (array $payload): array {
// your DB logic here
return ['id' => 123, 'total' => 99.99];
})
->register('user.find', function (array $payload): array {
return ['id' => $payload['id'], 'name' => 'Alice'];
});
// Call Node.js (e.g. generate a PDF)
$pdf = $bridge->call('pdf.generate', [
'template' => 'invoice',
'data' => ['invoiceId' => 123],
]);
// Start listener (blocking — run as a separate daemon)
$bridge->listen();
// app/Providers/AppServiceProvider.php
public function boot(PhpBridge $bridge): void
{
$bridge->register('invoice.create', function (array $p): array {
return Invoice::create($p)->toArray();
});
}
use PhpNodeBridge\Laravel\Bridge; // Facade
class DocumentController extends Controller
{
public function sign(Request $request)
{
$result = Bridge::call('esign.sign', $request->all());
return response()->json($result);
}
public function sendEmail(Request $request)
{
Bridge::fire('email.send', $request->all()); // fire-and-forget
return response()->json(['queued' => true]);
}
}