john
2018-11-23 16:16:56 UTC
Hello,
I have previously used lite based applications and am starting to get
familiar with full applications. I have been struggling to understand
how to implement Pg::PubSub across different parts of my application.
My questions probably apply to any publish/subscribe implementation. I
have things working (messages are received by the controller) however it
feels messy to me.
I have code like this in the controller:
package TelemControl::Controller::Main;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg;
use Mojo::Pg::PubSub;
our $pg = Mojo::Pg->new('postgresql://script@/db');
our $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
in the controller client handler:
my $cb = $pubsub->listen(
sensor_msg => sub {
my ( $pubsub, $payload ) = @_;
my $msg = decode_json($payload);
$c = $c->send( { json => $msg } );
$c->app->log->debug(
"item to let client know about (pubsub): $msg->{type}");
}
);
In my model:
sub new {
my ($class, $log, $config) = @_;
my $pg = Mojo::Pg->new('postgresql://script@/db')
or $log->error('Could not connect to database');
my $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
my $self = {
log => $log,
config => $config,
pubsub => $pubsub,
pg => $pg,
};
bless $self, $class;
return $self;
}
and then publish like this:
$self->{pubsub}->notify( sensor_msg => encode_json($results[0]) );
Questions:
- Is there a better approach to this? It would seem like with the
way I did the controller every client that connects may bring up an
additional database connection? Or is it bad for other reasons?
- I tried various ways to inject the pubsub into model and controllers
(e.g. helpers) but could not get that to work. Do I need to try
harder on that front?
Thanks,
John
I have previously used lite based applications and am starting to get
familiar with full applications. I have been struggling to understand
how to implement Pg::PubSub across different parts of my application.
My questions probably apply to any publish/subscribe implementation. I
have things working (messages are received by the controller) however it
feels messy to me.
I have code like this in the controller:
package TelemControl::Controller::Main;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg;
use Mojo::Pg::PubSub;
our $pg = Mojo::Pg->new('postgresql://script@/db');
our $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
in the controller client handler:
my $cb = $pubsub->listen(
sensor_msg => sub {
my ( $pubsub, $payload ) = @_;
my $msg = decode_json($payload);
$c = $c->send( { json => $msg } );
$c->app->log->debug(
"item to let client know about (pubsub): $msg->{type}");
}
);
In my model:
sub new {
my ($class, $log, $config) = @_;
my $pg = Mojo::Pg->new('postgresql://script@/db')
or $log->error('Could not connect to database');
my $pubsub = Mojo::Pg::PubSub->new(pg => $pg);
my $self = {
log => $log,
config => $config,
pubsub => $pubsub,
pg => $pg,
};
bless $self, $class;
return $self;
}
and then publish like this:
$self->{pubsub}->notify( sensor_msg => encode_json($results[0]) );
Questions:
- Is there a better approach to this? It would seem like with the
way I did the controller every client that connects may bring up an
additional database connection? Or is it bad for other reasons?
- I tried various ways to inject the pubsub into model and controllers
(e.g. helpers) but could not get that to work. Do I need to try
harder on that front?
Thanks,
John
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.