Build High‑Performance PHP RPC Services with Workerman‑JsonRpc

This guide introduces Workerman‑JsonRpc, a lightweight PHP RPC framework built on Workerman, detailing its key features, environment requirements, installation steps, synchronous and asynchronous client usage, server implementation, and built‑in monitoring capabilities for creating scalable network applications.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Build High‑Performance PHP RPC Services with Workerman‑JsonRpc

Overview

Workerman‑JsonRpc is a JSON‑based remote procedure call (RPC) framework that leverages the high‑performance PHP socket server capabilities of Workerman. It provides a lightweight and efficient solution for building scalable RPC services, chat systems, games, and other network‑driven applications.

Main Features

Multi‑process support : Efficiently manage multiple processes for concurrent tasks.

TCP/UDP support : Works with both TCP and UDP protocols for diverse scenarios.

Application‑layer protocol compatibility : Supports various application‑layer protocols.

High concurrency : Utilizes libevent for handling massive concurrent connections.

File change detection and auto‑reload : Automatically reloads when source files change, avoiding downtime.

Smooth restart : Enables seamless restarts for updates or configuration changes.

Telnet remote control and monitoring : Provides remote management via Telnet.

Exception monitoring and alerts : Built‑in tools track and alert on anomalies.

Long‑connection support : Persistent connections suitable for real‑time apps like chat or games.

Custom user process execution : Allows workers to run under specified user identities for enhanced security.

Environment Requirements

PHP version : Requires PHP 5.3+; only the CLI is needed, no PHP‑FPM, Nginx, or Apache.

Operating system : Compatible with Unix‑like systems (Linux, macOS); Windows is not supported.

Installation

Installing Workerman‑JsonRpc is straightforward:

Clone or download the repository:

git clone https://github.com/walkor/workerman-JsonRpc

Install dependencies: composer install Start the service in daemon mode:

php start.php start -d

Getting Started

Workerman‑JsonRpc supports both synchronous and asynchronous client calls.

Synchronous client example

<?php
include_once 'yourClientDir/RpcClient.php';

// Configure server list
$address_array = [
    'tcp://127.0.0.1:2015',
    'tcp://127.0.0.1:2015'
];
RpcClient::config($address_array);

$uid = 567;

// Instantiate User service client
$user_client = RpcClient::instance('User');

// Synchronous call to getInfoByUid
$result = $user_client->getInfoByUid($uid);

This code invokes the getInfoByUid method of the User class located at Applications/JsonRpc/Services/User.php on the server.

Asynchronous client example

<?php
include_once 'yourClientDir/RpcClient.php';

// Configure server list
$address_array = [
    'tcp://127.0.0.1:2015',
    'tcp://127.0.0.1:2015'
];
RpcClient::config($address_array);

$uid = 567;
$user_client = RpcClient::instance('User');

// Asynchronous calls
$user_client->asend_getInfoByUid($uid);
$user_client->asend_getEmail($uid);

// ... other business logic ...

// Retrieve asynchronous results when needed
$result1 = $user_client->arecv_getEmail($uid);
$result2 = $user_client->arecv_getInfoByUid($uid);

This approach lets the client continue processing other tasks while waiting for server responses, improving performance in high‑latency scenarios.

Server implementation

On the server side, services are defined as PHP classes under Applications/JsonRpc/Services. Each class contains static methods that can be called remotely. Example:

<?php
class User {
    public static function getInfoByUid($uid) {
        // Logic to fetch user info
        return ['uid' => $uid, 'name' => '示例用户'];
    }

    public static function getEmail($uid) {
        // Logic to fetch user email
        return '[email protected]';
    }
}

To add a new service, simply create a new class file in the Services directory and define the desired static methods.

Monitoring RPC Services

Workerman‑JsonRpc includes a built‑in monitoring page accessible at http://<server‑IP>:55757, which displays service status, performance metrics, and error information for easier management and debugging.

Conclusion

Workerman‑JsonRpc combines Workerman’s high‑performance socket server with JSON‑based RPC features, offering developers a powerful, flexible framework for building efficient, real‑time network applications. Its support for synchronous and asynchronous calls, smooth restarts, and built‑in monitoring makes it an excellent choice for modern PHP backend development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendRPChigh concurrencyPHPJSON-RPCWorkerman
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.