NAME

BoardStreams - WebSocket channels for Mojolicious whose events and state survive disconnections

SYNOPSIS

use Mojolicious::Lite -signatures;
use Future::AsyncAwait future_class => 'Mojo::Promise';

my $hub = plugin BoardStreams => { pg => 'postgresql://user@host/db' };

$hub->channel('counter:*',
    initial_state => { n => 0 },
    join          => sub ($ctx) { 1 },
    requests      => {
        add => async sub ($ctx, $payload) {
            await $ctx->update(sub ($u) {
                $u->state->{n} += $payload->{by};
                $u->emit(added => $payload);
            });
            return 'thanks';
        },
    },
);

# anywhere else in the application
$c->boardstreams->emit('counter:lobby', 'announcement', { text => 'hi' });

app->start;

TUTORIAL

This distribution comes with a step-by-step tutorial, TUTORIAL.md, in its top-level directory. It builds a small chat application and covers both the server and the browser side: channels, state, events, actions and requests, the JavaScript client, and what happens during disconnections.

Because it is a Markdown file, it is not listed among the documentation on MetaCPAN. Find it by browsing the files of the release there, in the unpacked distribution, or in the source repository at https://codeberg.org/gandalf/boardstreams-perl.

DESCRIPTION

Browsers join channels over one WebSocket (served at /boardstreams by default) with the boardstreams npm package. They send actions and requests; only the server emits events and changes a channel's state.

Every change gets a per-channel sequence number and is stored in PostgreSQL together with its events, in one transaction. A browser that reconnects says which number it saw last and is sent what it missed, or the whole state if that history has been purged (retention option, in seconds; default one hour). Actions and requests carry a unique id, so one that is resent after a reconnection takes effect once.

The npm package and this distribution are released under the same version number and must be the identical version to work together. The server announces its version to each browser, and the client refuses to go on if its own differs.

See examples/counter.pl for a complete application.

OPTIONS

plugin BoardStreams => { pg => $pg, retention => 86400 };

CHANNEL PATTERNS

The first argument of channel is a channel name, or a pattern containing *. A * stands for any run of characters other than a colon, possibly none: chat:* covers chat:lobby and chat: but not chat:eu:lobby, which needs chat:*:*. A ** stands for any run of characters at all, colons included, so chat:** covers all three. Both can appear anywhere and more than once; every other character is literal.

If several patterns cover a name, the most specific wins: an exact name, then the pattern with the most literal characters, then the one with the fewest **, then the one registered first.

HANDLERS

Handlers given to channel receive a BoardStreams::Context and may be async. join accepts by returning true; $ctx->reject($reason, $data) refuses a join or fails a request with something the browser can read. It runs again every time a browser rejoins. $ctx->c is the WebSocket's controller, for sessions and the like.

$ctx->update(sub ($u) { ... }) and $hub->update($channel, sub ($u) { ... }) change $u->state and queue events with $u->emit($name, $payload).

AUTHOR

Alexander Karelas alex.karelas@gmail.com

LICENSE

Copyright (C) Alexander Karelas.

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