How to Build High‑Performance RPC Services with Webman’s tinywan/rpc Plugin
This guide explains why RPC is needed, introduces the tinywan/rpc plugin for the Webman framework, and provides step‑by‑step installation, server and client code examples, response handling, error logging, advantages, and real‑world use cases for building fast, flexible backend services.
Why RPC?
Remote Procedure Call (RPC) allows a program on one server to invoke functions on another server over a network, mimicking local calls despite separate memory spaces.
Plugin Overview
The tinywan/rpc plugin is built for the Webman framework, leveraging its asynchronous, event‑driven architecture to provide a lightweight, fast RPC solution suitable for micro‑services, real‑time apps, and IoT.
Installation
Install via Composer: composer require tinywan/rpc The command downloads the package and integrates it into a Webman project; ensure the environment meets Webman's requirements.
Usage
Server‑side configuration
Create a service class under the service directory, e.g. service/User.php:
namespace service;
class User
{
public function get($args)
{
return response_rpc_json(0, '获取成功', $args);
}
}This class defines a get method that returns a JSON response with a status code, message, and the supplied arguments.
Client call
From a client, open a TCP socket to the RPC server and send a JSON request:
// Connect to RPC server
$client = stream_socket_client('tcp://127.0.0.1:9512', $errorCode, $errorMessage);
if (false === $client) {
throw new \Exception('rpc 连接失败: ' . $errorMessage);
}
// Prepare request data
$request = [
'class' => 'user',
'method' => 'get',
'args' => [
[
'uid' => 2023,
'username' => 'Tinywan',
]
]
];
// Send request (Text protocol requires newline at end)
fwrite($client, json_encode($request) . "
");
// Read response
$result = fgets($client, 10240000);
$result = json_decode($result, true);
var_export($result);Response examples
Successful call returns:
{
"code": 0,
"msg": "用户列表",
"data": {
"uid": 2025,
"username": "Tinywan"
}
}Calling a non‑existent class yields an error response such as:
{
"code": 404,
"msg": "接口调用类不存在",
"data": {}
}Error logging
The plugin logs detailed errors. For example, a missing database column generates a log entry like:
[2025-08-16 14:46:26] default.ERROR: RPC Service Exception Message SQLSTATE[42S22]: Column not found: 1054 Unknown column 'to_user_id' in 'where clause' {"error":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'to_user_id' in 'where clause'","file":"/var/www/db/PDOConnection.php","line":797} []Developers can use this information to quickly locate and fix the issue.
Plugin Advantages
Simple to use : abstracts network communication so remote calls feel like local method invocations.
High performance : built on Webman’s asynchronous, event‑driven model, handling concurrent requests efficiently.
Flexibility : fits micro‑services, real‑time communication, and IoT scenarios, extending PHP’s applicability.
Robust error handling : provides detailed logs for rapid debugging.
Real‑world Scenarios
Microservice architecture : cross‑server service calls for authentication or data processing.
Real‑time applications : chat, notifications, and other low‑latency features.
IoT systems : communication between servers and devices such as smart home appliances or shared‑bike platforms.
Screenshots
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
