MojoX::...:Service(3)
π 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
- fedora 43: perl-MojoX-JSON-RPC 0.13-14.fc43 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms