File Coverage

blib/lib/Mercury/Controller/PubSub.pm
Criterion Covered Total %
statement 20 25 80.0
branch 2 2 100.0
condition n/a
subroutine 5 6 83.3
pod 3 3 100.0
total 30 36 83.3


line stmt bran cond sub pod time code
1             package Mercury::Controller::PubSub;
2             our $VERSION = '0.016';
3             # ABSTRACT: Pub/sub message pattern controller
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod # myapp.pl
8             #pod use Mojolicious::Lite;
9             #pod plugin 'Mercury';
10             #pod websocket( '/pub/*topic' )
11             #pod ->to( controller => 'PubSub', action => 'pub' );
12             #pod websocket( '/sub/*topic' )
13             #pod ->to( controller => 'PubSub', action => 'sub' );
14             #pod
15             #pod =head1 DESCRIPTION
16             #pod
17             #pod This controller enables a Lsub pattern|Mercury::Pattern::PubSub> on
18             #pod a pair of endpoints (L and L.
19             #pod
20             #pod For more information on the pub/sub pattern, see L.
21             #pod
22             #pod =head1 SEE ALSO
23             #pod
24             #pod =over
25             #pod
26             #pod =item L
27             #pod
28             #pod =item L
29             #pod
30             #pod =item L
31             #pod
32             #pod =back
33             #pod
34             #pod =cut
35              
36 1     1   368304 use Mojo::Base 'Mojolicious::Controller';
  1         8  
  1         6  
37 1     1   6144 use Mercury::Pattern::PubSub;
  1         14  
  1         7  
38              
39             #pod =method publish
40             #pod
41             #pod $app->routes->websocket( '/pub/*topic' )
42             #pod ->to( controller => 'PubSub', action => 'publish' );
43             #pod
44             #pod Controller action to connect a websocket as a publisher. A publish
45             #pod client sends messages through the socket. The message will be sent to
46             #pod all of the connected subscribers.
47             #pod
48             #pod This endpoint requires a C in the stash.
49             #pod
50             #pod =cut
51              
52             sub publish {
53 1     1 1 15684 my ( $c ) = @_;
54 1         6 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
55 1         5 $pattern->add_publisher( $c->tx );
56 1         7 $c->rendered( 101 );
57             }
58              
59             #pod =method subscribe
60             #pod
61             #pod $app->routes->websocket( '/sub/*topic' )
62             #pod ->to( controller => 'PubSub', action => 'subscribe' );
63             #pod
64             #pod Controller action to connect a websocket as a subscriber. A subscriber
65             #pod will recieve every message sent by publishers.
66             #pod
67             #pod This endpoint requires a C in the stash.
68             #pod
69             #pod =cut
70              
71             sub subscribe {
72 2     2 1 22478 my ( $c ) = @_;
73 2         6 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
74 2         6 $pattern->add_subscriber( $c->tx );
75 2         6 $c->rendered( 101 );
76             }
77              
78             #pod =method post
79             #pod
80             #pod Post a new message to the given topic without subscribing or
81             #pod establishing a WebSocket connection. This allows new messages to be
82             #pod pushed by any HTTP client.
83             #pod
84             #pod =cut
85              
86             sub post {
87 0     0 1 0 my ( $c ) = @_;
88 0         0 my $topic = $c->stash( 'topic' );
89 0         0 my $pattern = $c->_pattern( $topic );
90 0         0 $pattern->send_message( $c->req->body );
91 0         0 $c->render(
92             status => 200,
93             text => '',
94             );
95             }
96              
97             #=method _pattern
98             #
99             # my $pattern = $c->_pattern( $topic );
100             #
101             # Get or create the L object for the given
102             # topic.
103             #
104             #=cut
105              
106             sub _pattern {
107 3     3   29 my ( $c, $topic ) = @_;
108 3         16 my $pattern = $c->mercury->pattern( PubSub => $topic );
109 3 100       19 if ( !$pattern ) {
110 1         7 $pattern = Mercury::Pattern::PubSub->new;
111 1         16 $c->mercury->pattern( PubSub => $topic => $pattern );
112             }
113 3         5 return $pattern;
114             }
115              
116             1;
117              
118             __END__