avto-dev/json-rpc-laravel

Json Rpc package
23,921 6
Install
composer require avto-dev/json-rpc-laravel
Latest Version:v2.8.0
PHP:^8.2
License:MIT
Last Updated:Jul 24, 2026
Links: GitHub  ·  Packagist
Maintainer: eldario

JSON-RPC 2.0

Version PHP Version Build Status Coverage Downloads count License

JSON-RPC 2.0 is a remote procedure call protocol encoded in JSON. It is a very simple protocol, defining only a few data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.

Install

Require this package with composer using the following command:

$ composer require avto-dev/json-rpc-laravel "^2.0"

Installed composer is required (how to install composer).

You need to fix the major version of package.

Usage example

Create routes

Register actions for your methods in ./routes/web.php using the facade RpcRouter:

<?php

use AvtoDev\JsonRpc\RpcRouter;
use YourNamespace\SomeController;

RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum');
RpcRouter::on('show_full_request', [SomeController::class, 'showInfo']);

This package already contains a simple controller implementation, which you can expand it or take it only as an example.

Add new route for RpcController:

<?php

use Illuminate\Support\Facades\Route;

Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');

Don't forget about to load RPC routes if you want to specify routes not in the file ./routes/web.php

Create RPC controller

Create a new controller containing procedures to be called by JSON request:

<?php

namespace YourNamespace;

use AvtoDev\JsonRpc\Requests\RequestInterface;

class SomeController
{
    /**
     * Get sum of array.
     *
     * @param RequestInterface $request
     *
     * @return int
     */
    public function sum(RequestInterface $request): int
    {
        return (int) \array_sum($request->getParams());
    }

    /**
     * Get info from request.
     *
     * @param RequestInterface $request
     *
     * @return mixed[]
     */
    public function showInfo(RequestInterface $request): array
    {
        return [
            'params'       => $request->getParams(),
            'notification' => $request->isNotification(),
            'method_name'  => $request->getMethod(),
            'request ID'   => $request->getId(),
        ];
    }
}

Send RPC-request

$ curl 'http://localhost/rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"please_sum_array_values","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": 55,
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'http://localhost/rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"show_full_request","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": {
        "params": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "notification": false,
        "method_name": "please_sum_array_values",
        "request ID": "UNIQ_REQUEST_ID"
    },
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'http://localhost/rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"undefined_method","id":"UNIQ_REQUEST_ID"}'

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32601,
        "message": "Method not found"
    },
    "id": "UNIQ_REQUEST_ID"
}

Events

When the Kernel@handle method is called, some events are fired:

Event class Description
ErroredRequestDetectedEvent Detected not valid request from stack
RequestHandledEvent Means the remote method was successfully called
RequestHandledExceptionEvent An exception was thrown while executing the request

If necessary, you can create a Listener on this event and display received messages for debug.

The EventServiceProvider included with your Laravel application provides a convenient place to register all of your application's event listeners. data.

ChangeLog

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

If you will find any package errors, please, make an issue in current repository.

License

This is open-sourced software licensed under the MIT License.

Related Packages

larananas/lumen-json-rpc

Framework-free JSON-RPC 2.0 server library for modern PHP.

172 3
barbushin/laravel-swoole-jsonrpc

JSON-RPC server and client based on swoole, for laravel and lumen framework.

8 1
greplab/laravel-jsonrpcsmd

This is a port of the greplab/jsonrpcsmd library for laravel 4.2.

23 0
cloudcreativity/laravel-json-api

JSON API (jsonapi.org) support for Laravel applications.

1,106,512 785

Version History

Version Released PHP Laravel License
v2.8.0 ^8.2 ~11.0 || ~12.0 || ~13.0 MIT
v2.7.0 ^8.2 ~10.0 || ~11.0 || ~12.0 MIT
v2.6.0 ^8.1 ~10.0 || ~11.0 MIT
v2.5.0 ^8.0.2 ~9.0 || ~10.0 MIT
v2.4.0 ^7.3 || ^8.0 ~6.0 || ~7.0 || ~8.0 || ~9.0 MIT
Pre-releases (2)
Version Released PHP Laravel License
v1.0.0-RC2 ^7.1.3 >=5.5.0 <5.9.0 || ~6.0 MIT
v1.0.0-RC1 ^7.1.3 >=5.5.0 <5.9.0 || ~6.0 MIT