MojoX::...:Service(3)

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

πŸ’– MojoX::JSON::RPC::Service β€” JSON RPC Service registration πŸ’–

okay bestie so this is a page from the User Contributed Perl Documentation collection, and the whole vibe is MojoX::JSON::RPC::Service πŸ’… (the page itself gets filed under MojoX::...:Service(3) because apparently we're too iconic for a full name in the header, iykyk πŸŽ€). Sit down, grab a strawberry drink πŸ“, and let's talk about registering JSON-RPC service calls.


✨ SYNOPSIS ✨

okay so first things first, you use it:

use MojoX::JSON::RPC::Service;

my $svc  = MojoX::JSON::RPC::Service->new;

$svc->register(
    'sum',
    sub {
        my @params = @_;
        my $sum    = 0;
        $sum += $_ for @params;
        return $sum;
    }
);

$svc->register(
    'remote_address',
    sub {
        my $tx = shift;
        return $tx->remote_address;
    },
    {
         with_mojo_tx => 1
    }
);

## Then in Mojolicious application
$self->plugin(
    'json_rpc_dispatcher',
    services => {
        '/jsonrpc'  => $svc,
    }
);

then this package can also be used as a base class so you can make object-oriented JSON-RPC apps and honestly? it's giving main character energy πŸ‘‘:

package MyService;

use Mojo::Base 'MojoX::JSON::RPC::Service';

sub sum {
    my ($self, @params) = @_;
    my $sum    = 0;
    $sum += $_ for @params;
    return $sum;
}

__PACKAGE__->register_rpc_method_names( 'sum' );

## Then in Mojolicious application
$self->plugin(
    'json_rpc_dispatcher',
    services => {
        '/jsonrpc'  => MyService->new,
    }
);

🌸 DESCRIPTION 🌸

Register JSON-RPC service calls. that's it, that's the whole description, no notes πŸ’–


πŸ¦‹ METHODS πŸ¦‹

MojoX::JSON::RPC::Service inherits all methods from Mojo::Base and implements the following new ones. (yes bestie, all of them, we're not gatekeeping 🌈)

πŸ’… register

Register RPC methods. like, literally just hand it a name and a coderef and it's done:

$svc->register(
    'sum',
    sub {
        my @params = @_;
        my $sum    = 0;
        $sum += $_ for @params;
        return $sum;
   }
);

with_mojo_tx can be passed as options. In that case, Mojo::Transaction object will be pass as first argument of the subroutine. (so instead of getting the params first, you get the whole transaction, and honestly? obsessed 😭)

$svc->register(
    'remote_address',
    sub {
        my $tx = shift;
        return $tx->remote_address;
    },
    {
         with_mojo_tx => 1
    }
);

✨ register_rpc_method_names

Class method. Register a list of methods as JSON-RPC calls. just name them all and let them cook πŸ”₯

__PACKAGE__->register_rpc_method_names( 'sum', 'multiply' );

πŸ‘€ SEE ALSO πŸ‘€

  • MojoX::JSON::RPC::Dispatcher β€” go look at it, it's the whole other half of this story, no cap 🫢

okay that's the whole page bestie, you ate that πŸ’– this little write-up came to you from perl v5.42.0, dated 2025-07-25, and it lives under the name MojoX::...:Service(3) β€” slay, bye, mwah πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from