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

ZeroMQ::PubSub::Server - Listen for published events and broadcast them to all connected subscribers

SYNOPSIS

use ZeroMQ::PubSub::Server;
my $server = ZeroMQ::PubSub::Server->new(
    # clients connect here to publish events
    publish_addrs => [ 'tcp://0.0.0.0:4000', 'ipc:///tmp/pub.sock' ],

    # clients connect here to subscribe to events
    subscribe_addrs => [ 'tcp://0.0.0.0:5000', 'ipc:///tmp/sub.sock' ],

    debug => 1,
);

# listen for events forever
{
    # listen for events being published to our server
    my $pub_sock = $self->bind_publish_socket;

    # set up to broadcast events to waiting subscribers
    my $sub_sock = $self->bind_subscribe_socket;

    # main processing loop
    while (1) {
        # block while we get one message
        my $msg = $server->recv;

        # deep clone $msg so that event handlers can't modify it
        my $orig = clone($msg);

        # run event handlers
        $self->dispatch_event($msg);

        # broadcast event to subscribers
        $server->broadcast($orig)
    }
}

# exact same as above
$server->run;

ATTRIBUTES

publish_addrs

ArrayRef of socket addresses to receive client event publishing requests

subscribe_addrs

ArrayRef of socket addresses that clients can connect to for receiving events

METHODS

bind_publish_socket

Bind publish socket to publish_addrs

bind_subscribe_socket

Listen for clients wishing to subscribe to published events

recv

Blocks and receives one event. Returns object parsed from JSON, or undef if failure.

broadcast($event)

Sends $event to all connected subscribers.

poll_once

Blocks and waits for a publish message, dispatches to event handlers, then broadcasts it to subscribers.

run

Runs pubsub server forever. See synopsis.

SEE ALSO

ZeroMQ::PubSub::Client, ZeroMQ::PubSub