The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::SQLite::PubSub - Publish/Subscribe

SYNOPSIS

  use Mojo::SQLite::PubSub;

  my $pubsub = Mojo::SQLite::PubSub->new(sqlite => $sql);
  my $cb = $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "Received: $payload";
  });
  $pubsub->notify(foo => 'I ♥ SQLite!');
  $pubsub->unlisten(foo => $cb);

DESCRIPTION

Mojo::SQLite::PubSub is a scalable implementation of the publish/subscribe pattern used by Mojo::SQLite. It allows many consumers to share the same database connection, to avoid many common scalability problems. As SQLite has no notification system, it is implemented via event loop polling in Mojo::SQLite::Database, using automatically created tables prefixed with mojo_pubsub.

All subscriptions will be reset automatically and the database connection re-established if a new process has been forked, this allows multiple processes to share the same Mojo::SQLite::PubSub object safely.

EVENTS

Mojo::SQLite::PubSub inherits all events from Mojo::EventEmitter and can emit the following new ones.

reconnect

  $pubsub->on(reconnect => sub {
    my ($pubsub, $db) = @_;
    ...
  });

Emitted after switching to a new database connection for sending and receiving notifications.

ATTRIBUTES

Mojo::SQLite::PubSub implements the following attributes.

poll_interval

  my $interval = $pubsub->poll_interval;
  $pubsub      = $pubsub->poll_interval(0.25);

Interval in seconds to poll for notifications from "notify", passed along to "notification_poll_interval" in Mojo::SQLite::Database. Note that lower values will increase pubsub responsiveness as well as CPU utilization.

sqlite

  my $sql = $pubsub->sqlite;
  $pubsub = $pubsub->sqlite(Mojo::SQLite->new);

Mojo::SQLite object this publish/subscribe container belongs to.

METHODS

Mojo::SQLite::PubSub inherits all methods from Mojo::EventEmitter and implements the following new ones.

json

  $pubsub = $pubsub->json('foo');

Activate automatic JSON encoding and decoding with "to_json" in Mojo::JSON and "from_json" in Mojo::JSON for a channel.

  # Send and receive data structures
  $pubsub->json('foo')->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say $payload->{bar};
  });
  $pubsub->notify(foo => {bar => 'I ♥ SQLite!'});

listen

  my $cb = $pubsub->listen(foo => sub {...});

Subscribe to a channel, there is no limit on how many subscribers a channel can have. Automatic decoding of JSON text to Perl values can be activated with "json".

  # Subscribe to the same channel twice
  $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "One: $payload";
  });
  $pubsub->listen(foo => sub {
    my ($pubsub, $payload) = @_;
    say "Two: $payload";
  });

notify

  $pubsub = $pubsub->notify('foo');
  $pubsub = $pubsub->notify(foo => 'I ♥ SQLite!');
  $pubsub = $pubsub->notify(foo => {bar => 'baz'});

Notify a channel. Automatic encoding of Perl values to JSON text can be activated with "json".

unlisten

  $pubsub = $pubsub->unlisten('foo');
  $pubsub = $pubsub->unlisten(foo => $cb);

Unsubscribe from a channel.

SEE ALSO

Mojo::SQLite, Mojo::SQLite::Database