NAME

BoardStreams::Client - Connect to a BoardStreams server from Perl

SYNOPSIS

use Mojo::Base -strict, -signatures;
use BoardStreams::Client;
use Mojo::IOLoop;

my $bs   = BoardStreams::Client->new(url => 'wss://example.com/boardstreams');
my $chat = $bs->join('chat:lobby');

# Runs with the current state as soon as it is known, then on every change.
$chat->state_o->subscribe(sub ($state) { say scalar $state->{messages}->@*, ' messages' });

# Payloads of the events named "typing".
$chat->on('typing')->subscribe(sub ($payload) { say "$payload->{name} is typing" });

# Fire and forget.
$chat->action(typing => { name => 'alice' });

# A promise of the response payload.
$chat->request(say => { name => 'alice', text => 'hello' })->then(
    sub ($response) { say "$response->{count} messages so far" },
    sub ($error)    { say 'failed: ', $error->reason },
);

Mojo::IOLoop->start;

DESCRIPTION

The Perl counterpart of the boardstreams JavaScript client: it joins channels of a BoardStreams server over one WebSocket, keeps a copy of each channel's state up to date, delivers the channel's events in order, and sends actions and requests. It is a port of the JavaScript client and behaves the same way:

  • After a disconnection it reconnects by itself, with a growing delay, and rejoins its channels. It is sent what it missed, and actions and requests made meanwhile are delivered once the connection is back, exactly once.

  • It gives up on a connection that has gone silent, judging by the server's heartbeats.

  • It only works with a server of exactly its own version. With any other it warns, refuses every join and request with the reason version_mismatch, and does not reconnect.

  • It does not reconnect after the server has disconnected it on purpose.

It runs on the Mojo::IOLoop event loop and connects with Mojo::UserAgent. Its streams are RxPerl observables, where the JavaScript client has RxJS ones; their names end in _o where the JavaScript names end in $. Promises are Mojo::Promise objects.

THE CLIENT

new

my $bs = BoardStreams::Client->new(url => 'ws://localhost:3000/boardstreams');

Connects at once. Options:

url

Required. The address of the server's WebSocket route.

min_delay

Seconds before the first reconnection attempt. Defaults to 0.5. The delay doubles with every failed attempt, and each actual delay is shortened at random by up to half.

max_delay

Upper limit for that delay, in seconds. Defaults to 30.

ua

A Mojo::UserAgent to connect with, for example one with TLS options or cookies set. By default the client makes its own.

Note that the JavaScript client takes these delays in milliseconds.

join

my $channel = $bs->join('chat:lobby');

Joins a channel and returns it, or returns the channel if it is already joined. The channel can be used straight away: actions and requests are held until the server confirms the join.

status_o

Observable of the connection's status: connecting, connected, disconnected or closed. Replays the current value.

close

Closes the connection for good and leaves all channels.

CHANNELS

name

The channel's name.

state_o

Observable of the channel's state. It replays the latest value to new subscribers and emits on every change. Each value is a new structure that shares the parts that did not change with the previous one; treat them as read-only.

state

The current state, or undef before the first join completes.

events_o

Observable of every event of the channel, in order, each a hash with name, payload and seq.

on

$channel->on('typing')->subscribe(sub ($payload) { ... });

Observable of the payloads of the events with the given name.

action

$channel->action($name, $payload);

Sends an action.

request

my $promise = $channel->request($name, $payload);

Sends a request and returns a promise of the response payload. When it resolves, state already includes whatever the request changed on this channel. It rejects with a "ERRORS" object if the request fails, the join is refused, or the channel is left.

joined

A promise that resolves once, when the channel is first joined, and rejects with an error object if the server refuses.

status_o

Observable of the channel's status: joining, joined, rejected or left. Replays the current value. It emits joined on every join, including each automatic rejoin after a reconnection, and joining while disconnected.

gaps_o

Emits when the client was away for longer than the server keeps history: the state is current again, but some events were missed.

leave

Leaves the channel. Unanswered requests are rejected.

ERRORS

Promises reject with a BoardStreams::Client::Error object. Its reason is a short string chosen by the server, such as forbidden, and its data is whatever the server attached to it. In a string it reads as BoardStreams: $reason.

A channel that the server removes this client from, or deletes, ends with the status rejected and the server's reason (kicked or deleted unless it gave another).

SEE ALSO

BoardStreams, and TUTORIAL.md in this distribution.