MojoX::JSON::RPC(3)

πŸ“¦
version 0.13 Β· 2 versions
, in fedora 43

✨ NAME ✨

MojoX::JSON::RPC β€” bestie, it's a Perl implementation of the JSON-RPC 2.0 protocol for Mojolicious πŸ’–

(yes you're literally reading the MojoX::JSON::RPC(3) man page rn, and yes it's giving main character energy πŸ‘‘)


🌸 SYNOPSIS 🌸

okay so there's three whole ways to vibe with this, and every single one is right here 🫢

πŸ–₯️ Server as a plugin (Mojolicious::Plugin::JsonRpcDispatcher)

#!/usr/bin/env perl
use Mojolicious::Lite;
use MojoX::JSON::RPC::Service;

plugin 'json_rpc_dispatcher' => {
    services => {
        '/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
            'sum',
            sub {
                my @params = @_;
                my $sum    = 0;
                $sum += $_ for @params;
                return $sum;
            }
        )
    }
};

app->start;

πŸ“ž Client (MojoX::JSON::RPC::Client)

#!/usr/bin/env perl
use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);

if($res) {
    if ($res->is_error) { # RPC ERROR
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

πŸ¦‹ Non-blocking client

#!/usr/bin/env perl
use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    if($res) {
        if ($res->is_error) { # RPC ERROR
            print 'Error : ', $res->error_message;
        }
        else {
            print $res->result;
        }
    }
    else {
       my $tx_res = $client->tx->res; # Mojo::Message::Response object
       print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
    }

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

πŸ’œ DESCRIPTION πŸ’œ

So basically this module implments a client plugin and a server plugin for JSON-RPC 2.0, for use with Mojolicious. Two for the price of one, we love a multitasking icon πŸ’…

And it follows the draft specficiation for JSON-RPC 2.0 β€” the whole lore lives at http://groups.google.com/group/json-rpc/web/json-rpc-2-0 if you wanna go read it yourself. Obsessed with the dedication, ngl πŸ“šβœ¨


πŸ” SEE ALSO πŸ”

the girlies you should also go check out:

  • Mojolicious::Plugin::JsonRpcDispatcher
  • MojoX::JSON::RPC::Dispatcher
  • MojoX::JSON::RPC::Client

πŸ‘‘ AUTHOR πŸ‘‘

Henry Tang πŸ’–


🫢 CREDITS 🫢

shoutout to the besties, fr fr:


Copyright (C) 2011-2016, Henry Tang.

MojoX::JSON::RPC is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.

⚠️ bestie, read that again β€” "as is", no promises, no warranties, not even the implied ones. It's giving "you're on your own and we said it out loud" 😭

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. 🌈


πŸ†˜ SUPPORT πŸ†˜

Dmitry Karasik <dmitry@karasik.eu.org> πŸŽ€


and that's the whole page, bestie β€” this was MojoX::JSON::RPC(3), and for the record this beauty shipped as perl v5.42.0 on 2025-07-25. go forth and JSON-RPC responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from