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]);
    }
}

// config/bundles.php
return [
    PhpNodeBridge\Symfony\PhpNodeBridgeBundle::class => ['all' => true],
];

use PhpNodeBridge\Bridge\PhpBridge;

class DocumentController extends AbstractController
{
    public function __construct(private PhpBridge $bridge) {}

    #[Route('/api/pdf', methods: ['POST'])]
    public function pdf(Request $request): JsonResponse
    {
        $result = $this->bridge->call('pdf.generate', json_decode($request->getContent(), true));
        return $this->json($result);
    }
}

Your PHP App                       Your Node App
(Laravel / Symfony / Slim)        (Express / Next / Nuxt)

   PhpBridge  ◄──── HTTP ────►    NodeBridge
   :5556                           :5555

   register() ← Node can call      register() ← PHP can call
   call()     → calls Node         call()     → calls PHP
bash
composer vendor:publish --tag=bridge-config
bash
php artisan bridge:listen
bash
php bin/console bridge:listen
ini
[Unit]
Description=PHP Node Bridge
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/html
ExecStart=/usr/bin/php artisan bridge:listen
Restart=always

[Install]
WantedBy=multi-user.target
yaml
services:
  php:
    build: ./php-app
    environment:
      NODE_BRIDGE_HOST: node
      BRIDGE_SECRET: ${BRIDGE_SECRET}
    ports:
      - "8000:8000"
      - "5556:5556"

  node:
    build: ./node-app
    environment:
      PHP_BRIDGE_HOST: php
      BRIDGE_SECRET: ${BRIDGE_SECRET}
    ports:
      - "3000:3000"
      - "5555:5555"
bash
php tests/run.php