newbie question mod_perl2 ajax

newbie question mod_perl2 ajax

am 16.08.2008 05:18:23 von Collin Monahan

I'm trying to learn how to use mod_perl2 along with Ajax. Everything was humming along until it seemed like perhaps Apache had a couple instances of my handler in memory, and what I had declared as module variables were existent in two different states. It seemed like Apache was handing off to a different handler each time my Ajax callbacks were invoked on browser/user input, and the handler chosen was somewhat at random. Perhaps I could try to exemplify what I was seeing.

package Handlers::Main;
....
# now I think these aren't necessarily per connection.
my $ajax_div1a_contents = "default text for div1/a";
my $ajax_div1a_state = 0; # default state
my $ajax_div1b_contents = "default text for div1/b";
my $ajax_div1b_state = 0; # default state
....
sub handler {
my $r = shift;
my $ajax = Apache2::Ajax->new($r);
$r->print($ajax->build_html());
return Apache2::Const::OK
}
....
sub Show_Form_sub {
# here I could try to get the request or connection for using notes
my $self = shift;
my $r = $self->r;
my $con = $r->connection;
my $convar = $con->notes->get('convarA');
# but I don't think it will help during the ajax callbacks
....
$html .= '';
}
....
sub fAjax {
my $x = shift; # this is the field value, not self
# don't know how to get the request rec in here
# nor the connection
# and trying to use module variables like
# those declared above seems buggy
# but I would like to know more about the module
# state inside here (for only this connection)
# than just the value of x.
}

One workaround I imagined was maintaining all the needed state for each module-session in a database table keyed by sessionid, but I have the hunch there must be a usual way to do this, and that that's not the way it is.

tia

Collin Monahan

Re: newbie question mod_perl2 ajax

am 16.08.2008 08:14:22 von dstroma

> From: "Collin Monahan"
>
> I'm trying to learn how to use mod_perl2 along with Ajax. Everything was
> humming along until it seemed like perhaps Apache had a couple instances
> of my handler in memory, and what I had declared as module variables were
> existent in two different states. It seemed like Apache was handing off to
> a different handler each time my Ajax callbacks were invoked on
> browser/user input, and the handler chosen was somewhat at random. Perhaps
> I could try to exemplify what I was seeing.
>

Under mod_perl, packages/scripts remain in memory across requests, for the
duration of the Apache child. So in something like this, each time the
handler is invoked, the $counter will continue growing with each new
request... whether it's after numerous requests by one user or numerous
users issuing one request each:

package Somepackage;
my/our $counter = 0;
sub handler {
$counter++; #
$r->print($counter);
}

The second issue is that, due to the stateless nature of HTTP, requests are
dispatched to Apache/mod_perl children at random. You can't store
information about a particular user or user's session in a perl variable and
expect that user's next request to be sent to the same Apache/mod_perl
process. This is why there are so many modules and techniques for storing
user session/state information using various files and databases. Ajax has
nothing to do with it, it does not change any of this. All Ajax does is send
"normal" HTTP requests to the server, just without reloading the entire
page.

Re: newbie question mod_perl2 ajax

am 16.08.2008 13:27:48 von Collin Monahan

> Under mod_perl, packages/scripts remain in memory across requests, for the
> duration of the Apache child.
> The second issue is that, due to the stateless nature of HTTP, requests are
> dispatched to Apache/mod_perl children at random. You can't store
> information about a particular user or user's session in a perl variable and
> expect that user's next request to be sent to the same Apache/mod_perl
> process. This is why there are so many modules and techniques for storing
> user session/state information using various files and databases.

Indeed, thanks. I let a javascript function receive the result from the Ajax perl function, rather than the

element directly. Then also in a