I need some help to dig into Mojo::Pg::PubSub

16 views
Skip to first unread message

Nacho B

unread,
Nov 1, 2020, 6:28:50 AM11/1/20
to Mojolicious
Hi, I am digging in the Mojo::Pg::PubSub possibilities, to use multiple channels, and I would like to fully understand data contained in this variable: $pubsub

  # Forward messages from PostgreSQL to the browser
  my $cb = sub ($pubsub, $message) { $c->send($message) };
  $c->pg->pubsub->listen(mojochat => $cb);
  
This code excerpt comes from Joel Berger Vuechat, a great example I have used to learn (https://github.com/jberger/VueChat/blob/master/ex/vue_chat_comp.pl)
  

At first I thought that $pubsub was part of the response from PostgreSQL, along with $message, so I printed a Dump:

$VAR1 = bless( {
                 'json' => {},
                 'chans' => {
                              'mojochat2' => [
                                               sub { "DUMMY" },
                                               sub { "DUMMY" }
                                             ],
                              'mojochat' => [
                                              sub { "DUMMY" },
                                              sub { "DUMMY" },
                                              sub { "DUMMY" },
                                              sub { "DUMMY" },
                                              sub { "DUMMY" }
                                            ]
                            },
                 'pg' => bless( {
                                  'pubsub' => $VAR1,
                                  'password' => '********',
                                  'database_class' => 'Mojo::Pg::Database',
                                  'events' => {},
                                  'username' => '********',
                                  'dsn' => 'dbi:Pg:dbname=********;host=********',
                                  'pid' => 30059,
                                  'options' => {
                                                 'RaiseError' => 1,
                                                 'AutoInactiveDestroy' => 1,
                                                 'AutoCommit' => 1,
                                                 'PrintError' => 0,
                                                 'PrintWarn' => 0
                                               }
                                }, 'Mojo::Pg' ),
                 'events' => {
                               'disconnect' => [
                                                 sub { "DUMMY" }
                                               ]
                             },
                 'db' => bless( {
                                  'events' => {
                                                'close' => [
                                                             sub { "DUMMY" }
                                                           ],
                                                'notification' => [
                                                                    sub { "DUMMY" }
                                                                  ]
                                              },
                                  'pg' => $VAR1->{'pg'},
                                  'dbh' => bless( {}, 'DBI::db' ),
                                  'handle' => \*Mojo::Pg::Database::__ANONIO__,
                                  'listen' => {
                                                'mojochat' => 2,
                                                'mojochat2' => 1,
                                                'mojo.pubsub' => 1
                                              },
                                  'watching' => 1
                                }, 'Mojo::Pg::Database' )
               }, 'Mojo::Pg::PubSub' );
               
               
               
Now I see that $pubsub is not coming from PostgreSQL. It is the current object from Mojolicious PubSub.      
               
In 'chans' I can see all the current channels (mojochat and mojochat2). Both have an array of current connections, and all the elements are exactly the same: 'sub { "DUMMY" }'.

So, when using Hypnotoad, each Worker will have its own arrays. Am I right?

I was trying to get the number of connected users, but they will be scattered through all the Workers, and I don't know how to access each worker.

I was also trying to find a way to send a message exclusively to one user, but the first step, before anything else, is to have an Id for each connection, and I only have "DUMMY". 

It should be great if someone walks me through this.

Regards:
Nacho B.



Reply all
Reply to author
Forward
0 new messages