farkhadbek has asked for the wisdom of the Perl Monks concerning the following question:


I would like to have chat-application, very similar to this websocket example:

#!/usr/bin/env perl use utf8; use Mojolicious::Lite; use DateTime; use Mojo::JSON; get '/' => 'index'; my $clients = {}; websocket '/echo' => sub { my $self = shift; app->log->debug(sprintf 'Client connected: %s', $self->tx); my $id = sprintf "%s", $self->tx; $clients->{$id} = $self->tx; $self->receive_message( sub { my ($self, $msg) = @_; my $json = Mojo::JSON->new; my $dt = DateTime->now( time_zone => 'Asia/Tokyo'); for (keys %$clients) { $clients->{$_}->send_message( $json->encode({ hms => $dt->hms, text => $msg, }) ); } } ); $self->finished( sub { app->log->debug('Client disconnected'); delete $clients->{$id}; } ); };

But this is not working when this aplication is running under hypnotad.

Problem is in 'global' varibale $clients where reference to client's tx is stored. $clients is not global enough and of course not shared between hypnotoad workers (processes).

To solve this problem, the author of this example offered use Database or KeyValueStore to save "reference to clients tx".

Can anybody suggest how to do it?

Any idea