Tobias Oetiker
and 1 contributors


MojoX::Dispatcher::Qooxdoo::Jsonrpc - Dispatcher for Qooxdoo Json Rpc Calls



 # lib/

 use base 'Mojolicious';
 use RpcService;

 sub startup {
    my $self = shift;
    # instantiate all services
    my $services= {
        Test => RpcService->new(),
    # add a route to the Qooxdoo dispatcher and route to it
    my $r = $self->routes;
    $r->route('/qooxdoo') -> to(
                services    => $services, 
                debug       => 0,
                namespace   => 'MojoX::Dispatcher::Qooxdoo'



MojoX::Dispatcher::Qooxdoo::Jsonrpc dispatches incoming rpc requests from a qooxdoo application to your services and renders a (hopefully) valid json reply.


This example exposes a service named "Test" in a folder "RpcService". The Mojo application is named "QooxdooServer". The scripts are in the 'example' directory. First create this application using "mojolicious generate app QooxdooServer".

Then, lets write the service:

Change to the root directory "qooxdoo_server" of your fresh Mojo-Application and make a dir named 'qooxdoo-services' for the services you want to expose.

Our "Test"-service could look like:

 package RpcService;

 use Mojo::Base -base;

 # if you want to access mojo specific information
 # provide a controller property, it will be set to the
 # current controller as the request is dispached.
 # see L<Mojolicious::Controller> for documentation.
 has 'controller';
 # MANDADROY access check method. The method is called right before the actual
 # method call, after assigning mojo_session and mojo_stash properties are set.
 # These can be used for providing dynamic access control

 our %access = (
    add => 1,

 sub allow_rpc_access {
    my $self = shift;
    my $method = shift;              
    # check if we can access
    return $access{$method};

 sub add{
    my $self = shift;
    my @params = @_;
    # Debug message on Mojo-server console (or log)
    print "Debug: $params[0] + $params[1]\n";
    # uncomment if you want to die without further handling
    # die;
    # uncomment if you want to die with a message in a hash
    # die {code => 20, message => "Test died on purpose :-)"};
    # uncomment if you want to die with your homemade error object 
    # die MyException->new(code=>123,message=>'stupid error message');
    my $result =  $params[0] + $params[1]
    return $result;    

 package MyException;
 use Mojo::Base -base;
 has 'code';
 has 'message';

The Dispatcher executes all calls to your service module within an eval wrapper and will send any execptions you generate within back to the qooxdoo application as well as into the Mojolicious logfile.

Now, lets write our application. Normally one would use the services of Mojolicious::Plugin::QooxdooJsonrpc for this. If you want to use the dipatcher directly, this is how it is done.

 package QooxdooServer;

 use strict;
 use warnings;
 use RpcService::Test;

 use Mojo::Base 'Mojolicious';

 # This method will run once at server start
 sub startup {
    my $self = shift;
    my $services= {
        Test => RpcService::Test->new(),
        # more services here
    # tell Mojo about your services:
    my $r = $self->routes;
    # this sends all requests for "/qooxdoo" in your Mojo server 
    # to our little dispatcher.
    # change this at your own taste.
        services    => $services, 
        namespace   => 'MojoX::Dispatcher::Qooxdoo'


Now start your Mojo Server by issuing script/QooxdooServer daemon. If you want to change any options, type script/QooxdooServer help.


MojoX::Dispatcher::Qooxdoo::Jsonrpc calls the allow_rpc_access method to check if rpc access should be allowed. The result of this request is NOT cached, so you can use this method to provide dynamic access control or even do initialization tasks that are required before handling each request.


Matthias Bloch, <>, Tobias Oetiker, <>.

This Module is sponsored by OETIKER+PARTNER AG


Copyright (C) 2010 by :m)


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.