| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::PubSub; | ||
| 5 | use base qw/Jifty::Plugin/; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::Plugin::PubSub - Event-based publish/subscribe framework | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | In F<etc/config.yml>: | ||
| 14 | |||
| 15 | Plugins: | ||
| 16 | - PubSub: {} | ||
| 17 | |||
| 18 | In a region: | ||
| 19 | |||
| 20 | Jifty->subs->update_on( topic => "some_event" ); | ||
| 21 | |||
| 22 | In a model: | ||
| 23 | |||
| 24 | Jifty->bus->publish( "some_event" ); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | =head2 Generating events | ||
| 29 | |||
| 30 | The most basic aspect of event-based communication is the publishing of | ||
| 31 | messages. This is done via: | ||
| 32 | |||
| 33 | Jifty->bus->publish( "some_event" => { | ||
| 34 | some_key => "data", | ||
| 35 | }); | ||
| 36 | |||
| 37 | This notifies all subscribers of the C<some_event> class with the | ||
| 38 | arbitrary payload specified. See L<AnyMQ> for more details of the | ||
| 39 | backend data bus. The C<type> key of the data provided is reserved for | ||
| 40 | internal routing. | ||
| 41 | |||
| 42 | |||
| 43 | =head2 Consuming events outside the webserver | ||
| 44 | |||
| 45 | C<<Jifty->bus>> is an L<AnyMQ> bus; as such, the standard ways of | ||
| 46 | consuming apply here: | ||
| 47 | |||
| 48 | my $listen = Jifty->bus->new_listener; | ||
| 49 | $listen->subscribe( Jifty->bus->topic("some_event") ); | ||
| 50 | $listen->poll( sub { | ||
| 51 | my ($data) = @_; | ||
| 52 | warn "Got some event with " . $data->{some_key}; | ||
| 53 | } | ||
| 54 | # Loop forever | ||
| 55 | AE::cv->recv; | ||
| 56 | |||
| 57 | |||
| 58 | =head2 Pushing updated regions to the client | ||
| 59 | |||
| 60 | A region can request that it should be updated in the client when an | ||
| 61 | event is received. At the most basic, this is done via: | ||
| 62 | |||
| 63 | Jifty->subs->update_on( topic => "some_event" ); | ||
| 64 | |||
| 65 | Events may trigger arbitrary other region updates, using: | ||
| 66 | |||
| 67 | Jifty->subs->add( | ||
| 68 | topic => "some_event", | ||
| 69 | region => "...", | ||
| 70 | path => "...", | ||
| 71 | # Any other arguments from Jifty::Form::Element | ||
| 72 | ); | ||
| 73 | |||
| 74 | When a region is rendered because it was triggered by an event, it will | ||
| 75 | be passed the triggering event in an C<event> variable. | ||
| 76 | |||
| 77 | |||
| 78 | =head2 Running javascript in the client in response to events | ||
| 79 | |||
| 80 | You may also subscribe the web browser directly to events. This is done | ||
| 81 | by calling C<Jifty->subs->add> with no region-relevant arguments, merely | ||
| 82 | the C<topic>: | ||
| 83 | |||
| 84 | Jifty->subs->add( topic => $_ ) for qw/ some_event other_event /; | ||
| 85 | |||
| 86 | Once the browser is subscribed, the events will be made available via | ||
| 87 | the global C<pubsub> object in javascript, and can be consumed via | ||
| 88 | C<bind>: | ||
| 89 | |||
| 90 | jQuery(pubsub).bind("message.some_event", function (event, data) { | ||
| 91 | alert(data.some_key); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 Sending messages from javascript | ||
| 95 | |||
| 96 | From javascript in the client, you may also send information back to the | ||
| 97 | server via the global C<pubsub> object: | ||
| 98 | |||
| 99 | pubsub.send({type: 'something', data: 'here'}}); | ||
| 100 | |||
| 101 | In order to act on these responses, create a C<YourApp::PubSub> which | ||
| 102 | inherits from L<Jifty::Plugin::PubSub::Connection>, and override | ||
| 103 | L<Jifty::Plugin::PubSub::Connection/receive>: | ||
| 104 | |||
| 105 | package YourApp::PubSub; | ||
| 106 | use base qw/ Jifty::Plugin::PubSub::Connection /; | ||
| 107 | sub receive { | ||
| 108 | my $self = shift; | ||
| 109 | my $msg = shift; | ||
| 110 | return 1 if $self->SUPER::receive( $msg ); | ||
| 111 | warn "Got some message from the client: " . $msg->{data}; | ||
| 112 | return 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | Note that, for security reasons, this communication from the web browser | ||
| 116 | is B<not> published to the Jifty event bus (though you may opt to | ||
| 117 | republish them there so manually). | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | use AnyMQ; | ||
| 122 | use Plack::Builder; | ||
| 123 | use Web::Hippie::App::JSFiles; | ||
| 124 | use Jifty::Plugin::PubSub::Bus; | ||
| 125 | use Jifty::Plugin::PubSub::Connection; | ||
| 126 | use Jifty::Plugin::PubSub::Subscriptions; | ||
| 127 | |||
| 128 | our $VERSION = '0.5'; | ||
| 129 | |||
| 130 | =head1 METHODS | ||
| 131 | |||
| 132 | =head2 init | ||
| 133 | |||
| 134 | When initializing the plugin, it accepts any arguments that | ||
| 135 | L<AnyMQ/new_with_traits> accepts. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub init { | ||
| 140 | my $self = shift; | ||
| 141 | my %opt = @_; | ||
| 142 | |||
| 143 | Jifty->web->add_javascript( | ||
| 144 | qw! | ||
| 145 | pubsub/DUI.js | ||
| 146 | pubsub/Stream.js | ||
| 147 | pubsub/hippie.js | ||
| 148 | pubsub/hippie.pipe.js | ||
| 149 | pubsub/jquery.ev.js | ||
| 150 | pubsub.js | ||
| 151 | ! ); | ||
| 152 | |||
| 153 | $opt{connection} ||= Jifty->app_class({require => 0}, 'PubSub'); | ||
| 154 | $opt{connection} = 'Jifty::Plugin::PubSub::Connection' | ||
| 155 | unless Jifty::Util->try_to_require($opt{connection}); | ||
| 156 | $self->{connection} = delete $opt{connection}; | ||
| 157 | |||
| 158 | my $anymq = Jifty::Plugin::PubSub::Bus->new_with_traits( | ||
| 159 | traits => ['AMQP'], | ||
| 160 | host => 'localhost', | ||
| 161 | port => 5672, | ||
| 162 | user => 'guest', | ||
| 163 | pass => 'guest', | ||
| 164 | vhost => '/', | ||
| 165 | exchange => 'events', | ||
| 166 | %opt, | ||
| 167 | ); | ||
| 168 | *Jifty::bus = sub { $anymq }; | ||
| 169 | |||
| 170 | my $subs = Jifty::Plugin::PubSub::Subscriptions->_new; | ||
| 171 | *Jifty::subs = sub { $subs }; | ||
| 172 | |||
| 173 | Jifty::View->add_trigger( | ||
| 174 | body_end => sub { $self->body_end } | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 new_request | ||
| 179 | |||
| 180 | Part of the L<Jifty::Plugin> interface; clears out the | ||
| 181 | L<Jifty::Plugin::PubSub::Subscriptions> on every request. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub new_request { | ||
| 186 | Jifty->subs->reset; | ||
| 187 | } | ||
| 188 | |||
| 189 | =head2 body_end | ||
| 190 | |||
| 191 | Part of the L<Jifty::Plugin> interface; appends a snippet of javascript | ||
| 192 | to start the client-side websocket. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub body_end { | ||
| 197 | my $self = shift; | ||
| 198 | my $client_id = Jifty->subs->client_id || ""; | ||
| 199 | $client_id = "'$client_id'" if $client_id; | ||
| 200 | Jifty->web->out( qq|<script type="text/javascript">pubsub_init($client_id)</script>|); | ||
| 201 | } | ||
| 202 | |||
| 203 | =head2 psgi_app_static | ||
| 204 | |||
| 205 | Part of the L<Jifty::Plugin> interface; provides the required static | ||
| 206 | javascript. | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | sub psgi_app_static { | ||
| 211 | my $self = shift; | ||
| 212 | my $static_root = $self->static_root; | ||
| 213 | builder { | ||
| 214 | mount '/' => Plack::App::File->new(root => $static_root)->to_app; | ||
| 215 | mount '/js/pubsub' => Web::Hippie::App::JSFiles->new->to_app; | ||
| 216 | }; | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 wrap | ||
| 220 | |||
| 221 | Part of the L<Jifty::Plugin> interface; wraps the application to provide | ||
| 222 | websocket support, via L<Web::Hippie>, and binds it to the L<AnyMQ> bus | ||
| 223 | via L<Web::Hippie::Pipe>. | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub wrap { | ||
| 228 | my $self = shift; | ||
| 229 | my $app = shift; | ||
| 230 | |||
| 231 | my %connections; | ||
| 232 | builder { | ||
| 233 | mount '/__jifty/_hippie' => builder { | ||
| 234 | enable "+Web::Hippie"; | ||
| 235 | enable "+Web::Hippie::Pipe", bus => Jifty->bus; | ||
| 236 | sub { my $env = shift; | ||
| 237 | my $listener = $env->{'hippie.listener'}; # AnyMQ::Queue | ||
| 238 | my $client_id = $env->{'hippie.client_id'}; # client id | ||
| 239 | |||
| 240 | $connections{$client_id} | ||
| 241 | ||= $self->{connection}->_new($env); | ||
| 242 | my $c = $connections{$client_id}; | ||
| 243 | |||
| 244 | local $Jifty::WEB = $c->web; | ||
| 245 | local $Jifty::API = $c->api; | ||
| 246 | Jifty::Record->flush_cache if Jifty::Record->can('flush_cache'); | ||
| 247 | |||
| 248 | my $path = $env->{PATH_INFO}; | ||
| 249 | if ($path eq "/new_listener") { | ||
| 250 | $c->connect; | ||
| 251 | } elsif ($path eq "/message") { | ||
| 252 | $c->receive($env->{'hippie.message'}); | ||
| 253 | } elsif ($path eq "/error") { | ||
| 254 | delete $connections{$client_id}; | ||
| 255 | $c->disconnect; | ||
| 256 | } | ||
| 257 | }; | ||
| 258 | }; | ||
| 259 | |||
| 260 | mount '/' => $app; | ||
| 261 | }; | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; |