Back to documentation
package Mercury::Command::mercury::broker;
# ABSTRACT: Mercury message broker command


  Usage: mercury broker [OPTIONS]

    mercury broker
    mercury broker -m production -l http://*:8080
    mercury broker -l -l https://[::]:8081
    mercury broker -l 'https://*:443?cert=./server.crt&key=./server.key'

    -m, --mode <mode>                    Set the mode, defaults to the value
                                         of MOJO_MODE, PLACK_ENV, or 
    -b, --backlog <size>                 Listen backlog size, defaults to
    -c, --clients <number>               Maximum number of concurrent
                                         connections, defaults to 1000
    -i, --inactivity-timeout <seconds>   Inactivity timeout, defaults to 20
    -l, --listen <location>              One or more locations you want to
                                         listen on, defaults to the value of
                                         MOJO_LISTEN or "http://*:3000"
    -p, --proxy                          Activate reverse proxy support,
                                         defaults to the value of


L<Mercury::Command::broker> starts the L<Mercury> application.


use Mojo::Base 'Mojolicious::Command';
use Mercury;

use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::Daemon;

=attr description

    my $description = $cmd->description;
    $cmd = $cmd->description('Foo');

Short description of this command, used for the command list.


has description => 'Start WebSocket message broker';

=attr usage

    my $usage = $cmd->usage;
    $cmd = $cmd->usage('Foo');

Usage information for this command, used for the help screen.


has usage => sub { shift->extract_usage };

=method run


Run this command.


sub run {
    my ( $self, @args ) = @_;

    # If we're started with the 'mercury' script, we're our own app and we need
    # to serve ourselves. This ensures that the initialization already done
    # isn't done twice.
    my $app = $self->app->isa( 'Mercury' ) ? $self->app : Mercury->new;

    my $daemon = Mojo::Server::Daemon->new(
        app => $app,
        inactivity_timeout => 1200,

    GetOptionsFromArray( \@args,
        'b|backlog=i' => sub { $daemon->backlog($_[1]) },
        'c|clients=i' => sub { $daemon->max_clients($_[1]) },
        'i|inactivity-timeout=i' => sub { $daemon->inactivity_timeout($_[1]) },
        'l|listen=s' => \my @listen,
        'p|proxy' => sub { $daemon->reverse_proxy(1) },

    if ( @listen ) {
    elsif ( my $conf = eval { $app->config->{ broker } } ) {
        if ( $conf->{listen} ) {
            $daemon->listen( [ $conf->{listen} ] );



=head1 SEE ALSO

=over 4

=item *