elegant reactive services in Raku

2 min read Original article ↗

Welcome to Cro

Cro is a set of libraries for building reactive distributed systems, lovingly crafted to take advantage of all Raku has to offer. The high level APIs make the easy things easy, and the asynchronous pipeline concept at Cro's heart makes the hard things possible.

Ready to get started? Just zef install --/test cro and check out the documentation.

Set up your routes, and serve them

use Cro::HTTP::Router;
use Cro::HTTP::Server;
my $application = route {
    get -> 'greet', $name {
        content 'text/plain', "Hello, $name!";
    }
}
my Cro::Service $hello = Cro::HTTP::Server.new:
    :host<localhost>, :port<10000>, :$application;
$hello.start;
react whenever signal(SIGINT) { $hello.stop; exit; }

Built-in HTTP server

HTTP/1.1 persistent connections

HTTP/2.0 (use negotiated with ALPN)

HTTPS support

Add middleware at the server level

Configure body parsers and serializers

Requests dispatched to the thread pool, to make use of multi-core CPUs

Flexible request router

# Capture/constrain root segments with a signature.
get -> 'product', UUIDv4 $id {
    content 'application/json', get-product($id);
}
# Slurp root segments and serve static content
get -> 'css', *@path {
    static 'assets/css', @path;
}
# Get stuff from the query string
get -> 'search', :$term! {
    content 'application/json', do-search($term);
}

Pluggable body parsing and serialization

Enjoy built-in support for www-form-urlencoded, multi-part, and JSON bodies - or plug in your own body parsers and serializers.

put -> 'order', UUIDv4 $id {
    request-body -> %json-object {
        # Save it, and then...
        content 'application/json', %json-object;
    }
}

HTTP client included

Asynchronous API for getting the response and the body (body delivered all at once or streaming)

Supports HTTP/1.1 persistent connections

HTTPS, with optional custom certificate authority

HTTP/2.0 support, with request multiplexing

Pluggable body parsers/serializers - the same used by the server - and middleware support

Previous Next

Neatly integrated with the HTTP router

my $chat = Supplier.new;
get -> 'chat' {
    web-socket -> $incoming {
        supply {
            whenever $incoming -> $message {
                $chat.emit(await $message.body-text);
            }
            whenever $chat -> $text {
                emit $text;
            }
        }
    }
}

Web socket client

A Supply-based web services client is included in Cro.

It's just what you need for using web sockets between services, writing web socket clients, or writing integration tests for services exposed by a web socket.

Previous Next