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.
Installation:
composer require cravler/dnode
Require the autoloader:
require 'vendor/autoload.php';
Basic Server:
use DNode\Server;
$server = new Server();
$server->addMethod('add', function($a, $b) {
return $a + $b;
});
$server->listen(1337); // Port 1337
Basic Client:
use DNode\Client;
$client = new Client('127.0.0.1', 1337);
$result = $client->call('add', [2, 3]); // Returns 5
Service-Oriented Architecture:
authenticate, processOrder).$server->addMethod('authenticate', function($credentials) {
return User::validate($credentials);
});
Client-Side Integration:
$client = new Client('node-server', 3000);
$data = $client->call('fetchUser', [$userId]);
Event-Driven Communication:
$server->on('user:created', function($user) {
// Broadcast to clients
});
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());
}
PHP 5.3 Dependency:
php:5.3).No Encryption:
stream_socket_client('tls://...')) for security.Blocking Calls:
$server->addMethod('longTask', function($data) {
dispatch(new ProcessTask($data));
return 'queued';
});
Serialization Limits:
$server->setDebug(true);
telnet localhost 1337.Custom Protocols:
Extend DNode\Protocol for custom serialization (e.g., Protocol Buffers).
Authentication: Add middleware to validate requests:
$server->addMiddleware(function($request) {
if (!$request->hasHeader('X-API-KEY')) {
throw new Exception('Missing API key');
}
});
Performance:
stream_set_timeout() to avoid hanging:
stream_set_timeout($socket, 5); // 5-second timeout
How can I help you explore Laravel packages today?