Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dnode Laravel Package

cravler/dnode

PHP implementation of the DNode RPC protocol to let PHP and Node.js call each other’s functions over TCP sockets. Includes Composer install and cross-language examples (PHP↔PHP, Node↔Node, PHP↔Node). Current limitation: no encryption/SSL.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require cravler/dnode
    

    Require the autoloader:

    require 'vendor/autoload.php';
    
  2. Basic Server:

    use DNode\Server;
    
    $server = new Server();
    $server->addMethod('add', function($a, $b) {
        return $a + $b;
    });
    $server->listen(1337); // Port 1337
    
  3. Basic Client:

    use DNode\Client;
    
    $client = new Client('127.0.0.1', 1337);
    $result = $client->call('add', [2, 3]); // Returns 5
    

First Use Case

  • Cross-language RPC: Call PHP functions from Node.js or vice versa.
  • Example: Use PHP as a backend service for Node.js apps (e.g., heavy computations, legacy systems).

Implementation Patterns

Workflows

  1. Service-Oriented Architecture:

    • Expose PHP functions as RPC endpoints (e.g., authenticate, processOrder).
    • Example:
      $server->addMethod('authenticate', function($credentials) {
          return User::validate($credentials);
      });
      
  2. Client-Side Integration:

    • Use the client to call remote methods in Node.js or other PHP services.
    • Example:
      $client = new Client('node-server', 3000);
      $data = $client->call('fetchUser', [$userId]);
      
  3. Event-Driven Communication:

    • Use DNode’s pub/sub pattern for real-time updates (e.g., notifications).
    • Example:
      $server->on('user:created', function($user) {
          // Broadcast to clients
      });
      

Integration Tips

  • Laravel Service Providers: Bind the DNode server/client to the container for dependency injection:

    $this->app->singleton('dnode.server', function() {
        $server = new Server();
        $server->addMethod('laravelMethod', function() { ... });
        return $server;
    });
    
  • Middleware: Add validation/auth middleware before RPC calls:

    $server->addMethod('secureAction', function($data) {
        if (!auth()->check()) throw new Exception('Unauthorized');
        return process($data);
    });
    
  • Error Handling: Wrap RPC calls in try-catch blocks:

    try {
        $result = $client->call('riskyMethod', [$input]);
    } catch (Exception $e) {
        log::error($e->getMessage());
    }
    

Gotchas and Tips

Pitfalls

  1. PHP 5.3 Dependency:

    • Only works with PHP 5.3+. Use a compatible environment (e.g., Docker with php:5.3).
  2. No Encryption:

    • Data is sent in plaintext. Use TLS wrappers (e.g., stream_socket_client('tls://...')) for security.
  3. Blocking Calls:

    • RPC calls block execution. Use queues for long-running tasks:
      $server->addMethod('longTask', function($data) {
          dispatch(new ProcessTask($data));
          return 'queued';
      });
      
  4. Serialization Limits:

    • Complex objects (e.g., closures, resources) may fail. Stick to JSON-serializable data.

Debugging

  • Logging: Enable debug mode for verbose output:
    $server->setDebug(true);
    
  • Connection Issues: Check firewall rules and port availability. Test with telnet localhost 1337.

Extension Points

  1. Custom Protocols: Extend DNode\Protocol for custom serialization (e.g., Protocol Buffers).

  2. Authentication: Add middleware to validate requests:

    $server->addMiddleware(function($request) {
        if (!$request->hasHeader('X-API-KEY')) {
            throw new Exception('Missing API key');
        }
    });
    
  3. Performance:

    • Reuse client connections for repeated calls.
    • Use stream_set_timeout() to avoid hanging:
      stream_set_timeout($socket, 5); // 5-second timeout
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky