Giter Site home page Giter Site logo

msgpack-rpc's Introduction

NAME

MsgPack::RPC - MessagePack RPC client

VERSION

version 2.0.2

SYNOPSIS

use MsgPack::RPC;

my $rpc = MsgPack::RPC->new( io => '127.0.0.1:6666' );

$rpc->notify( 'something' => [ 'with', 'args' ] );

$rpc->request(
    request_method => [ 'some', 'args' ]
)->on_done(sub{
    print "replied with: ", @_;
});

$rpc->loop;

DESCRIPTION

MsgPack::RPC implements a MessagePack RPC client following the protocol described at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md.

METHODS

new( %args )

  • io( $socket )

  • io( [ $in_fh, $out_fh ] )

    Required. Defines which IO on which the MessagePack messages will be received and sent.

    The IO can be a local socket (e.g., /tmp/rpc.socket ), a network socket (e.g., 127.0.0.1:6543), or a pair of filehandles.

io()

Returns the IO descriptor(s) used by the object.

request( $method, $args, $id )

Sends the request. The $id is optional, and will be automatically assigned from an internal self-incrementing list if not given.

Returns a promise that will be fulfilled once a response is received. The response can be either a success or a failure, and in both case the fulfilled promise will be given whatever values are passed in the response.

$rpc->request( 'ls', [ '/home', '/tmp' ] )
    ->on_done(sub{ say for @_ })
    ->on_fail(sub{ die "couldn't read directories: ", @_ });

notify( $method, $args )

Sends a notification.

subscribe( $event_name, \&callback )

# 'ping' is a request
$rpc->subscribe( ping => sub($msg) {
    $msg->response->done('pong');
});

# 'log' is a notification
$rpc->subscribe( log => sub($msg) {
    print {$fh} @{$msg->args};
});

Register a callback for the given event. If a notification or a request matching the event is received, the callback will be called. The callback will be passed either a MsgPack::RPC::Message (if triggered by a notification) or MsgPack::RPC::Message::Request object.

Events can have any number of callbacks assigned to them.

The subscription system is implemented using the Beam::Emitter role.

loop( $end_condition )

Reads and process messages from the incoming stream, endlessly if not be given an optional $end_condition. The end condition can be given a number of messages to read, or a promise that will end the loop once fulfilled.

# loop until we get a response from our request

my $response = $rpc->request('add', [1,2] );

$response->on_done(sub{ print "sum is ", @_ });

$rpc->loop($response);


# loop 100 times
$rpc->loop(100);

SEE ALSO

AUTHOR

Yanick Champoux [email protected] endorse

COPYRIGHT AND LICENSE

This software is copyright (c) 2019, 2017, 2016, 2015 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

msgpack-rpc's People

Contributors

yanick avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.