Discussion:
[Mojolicious] Pg::PubSub in full application
john
2018-11-23 16:16:56 UTC
Permalink
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
--
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.
sri
2018-11-23 16:24:45 UTC
Permalink
Please avoid using Mojo::Pg::PubSub for now. It is broken and on the
verge of getting deprecated.

https://github.com/mojolicious/mojo-pg/issues/50

I've been meaning to do it two weeks ago, but members of the
community have asked me to wait and give them a chance to fix it.
Unfortunately that has not happened, so it's likely going away soon.

--
sebastian
--
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.
sri
2018-11-24 17:09:21 UTC
Permalink
Post by sri
Please avoid using Mojo::Pg::PubSub for now. It is broken and on the
verge of getting deprecated.
https://github.com/mojolicious/mojo-pg/issues/50
I've been meaning to do it two weeks ago, but members of the
community have asked me to wait and give them a chance to fix it.
Unfortunately that has not happened, so it's likely going away soon.
This might now be resolved with the 4.12 release.

https://metacpan.org/release/SRI/Mojo-Pg-4.12

--
sebastian
--
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.
Stefan Adams
2018-11-24 17:16:27 UTC
Permalink
Post by sri
This might now be resolved with the 4.12 release.
https://metacpan.org/release/SRI/Mojo-Pg-4.12
Yay!! I'm so glad this didn't get deprecated!!
--
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.
sri
2018-11-24 17:13:10 UTC
Permalink
Post by 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.
You don't implement anything. Just use Mojo::Pg like in the blog example
and then in your controllers "$c->pg->pubsub->...". That's the whole
reason it exists.

--
sebastian
--
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.
sri
2018-11-24 17:14:03 UTC
Permalink
... like in the blog example...
Forgot the link.

https://github.com/mojolicious/mojo-pg/tree/master/examples/blog

--
sebastian
--
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.
Loading...