Tatsumaki::MessageQueue(3)

NAME
Tatsumaki::MessageQueue - Message Queue system for Tatsumaki

SYNOPSIS
To publish a message, you first create an instance of the message queue on a specific channel:

    my $mq = Tatsumaki::MessageQueue->instance($channel);
    $mq->publish({
        type => "message", data => $your_data,
        address => $self->request->address,
        time => scalar Time::HiRes::gettimeofday,
    });

Later, in a handler, you can poll for new messages:

    my $mq = Tatsumaki::MessageQueue->instance($channel);
    my $client_id = $self->request->param('client_id')
        or Tatsumaki::Error::HTTP->throw(500, "'client_id' needed");
    $mq->poll_once($client_id, sub { $self->write(\@_); $self->finish; });

Additionally, if you are using Multipart XmlHttpRequest (MXHR) you can use the event API, and run a callback each time a new message is published:

    my $mq = Tatsumaki::MessageQueue->instance($channel);
    $mq->poll($client_id, sub {
        my @events = @_;
        for my $event (@events) {
            $self->stream_write($event);
        }
    });

DESCRIPTION
Tatsumaki::MessageQueue is a simple message queue, storing all messages in memory, and keeping track of a configurable backlog. All polling requests are made with a $client_id, and the message queue keeps track of a buffer per client, to ensure proper message delivery.

CONFIGURATION

BacklogLength
    To configure the number of messages in the backlog, set $Tatsumaki::MessageQueue::BacklogLength. By default, this is set to 30.

METHODS

  publish
This method publishes a message into the message queue, for immediate consumption by all polling clients.

  poll($client_id, $code_ref)
This is the event-driven poll mechanism, which accepts a callback as the second parameter. It will stream messages to the code ref passed in.

  poll_once($client_id, $code_ref)
This method returns all messages since the last poll to the code reference passed as the second parameter.

AUTHOR
Tatsuhiko Miyagawa

SEE ALSO
Tatsumaki