Trigger object creation to send message to actor

5 views
Skip to first unread message

Tobias Daub

unread,
Jul 6, 2009, 10:56:45 AM7/6/09
to lif...@googlegroups.com
Hi List,

I wanna send a message to an Actor everytime a new "row" is created in a
specific table. The Actor is responsible to refresh a HTML page and
display all the items from the table.

How do I do that?

thanks.

David Pollak

unread,
Jul 9, 2009, 5:36:15 PM7/9/09
to lif...@googlegroups.com

In the Meta object for the model you want to track, override afterCreate to call a method that sends the row to an Actor:

object Dog extends Dog with LongKeyedMetaMapper[Dog] with CRUDify[Long, Dog] {
  override def afterCreate = createdRow _ :: super.afterCreate

  private def createdRow(r: Dog) {
    DogBroker ! r
  }
}

The actor redistributes the update to all its listeners:

object DogBroker extends Actor with ListenerManager {
  var latestDog: Box[Dog] = Empty

  def createUpdate = latestDog

  override def highPriority = {
    case d: Dog =>
      latestDog = Full(d)
      updateListeners()
  }

  this.start
}

And the comet component looks like:

class Woof extends CometActor with CometListener {
  private var dog: Box[Dog] = Empty

  def render = <div>Dog: {dog.map(_.toString) openOr "None"}</div>

  def registerWith = DogBroker

  override def highPriority = {
    case Full(d: Dog) =>
      dog = Full(d)
      reRender(false)
  }
}

The full running source can be found at http://github.com/dpp/lift_1_1_sample/tree/record_ping

Thanks,

David



 




thanks.





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Peter Robinett

unread,
Jul 21, 2009, 5:04:16 PM7/21/09
to Lift
Hi David,

I'm looking to use your code in my application but first I wanted to
ask you about Scala Actors versus LiftActors. I see you use the Scala
ones here. Why is that? Would it be better to use your Lift ones? I'm
expecting object creation in my app every few seconds and when
responding it would like to update Flot charts and various other
components using Comet.

Thanks for your advice,
Peter Robinett

On Jul 9, 2:36 pm, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> The full running source can be found athttp://github.com/dpp/lift_1_1_sample/tree/record_ping
>
> Thanks,
>
> David
>
>
>
> > thanks.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Jul 22, 2009, 12:30:57 PM7/22/09
to lif...@googlegroups.com
On Tue, Jul 21, 2009 at 2:04 PM, Peter Robinett <pe...@bubblefoundry.com> wrote:

Hi David,

I'm looking to use your code in my application but first I wanted to
ask you about Scala Actors versus LiftActors. I see you use the Scala
ones here. Why is that? Would it be better to use your Lift ones? I'm
expecting object creation in my app every few seconds and when
responding it would like to update Flot charts and various other
components using Comet.

The issue with Scala Actors is the number of Actor creation/destruction operations.  You'll create an Actor per session... not a lot of cycling.

I'd go with Scala Actors.
 



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

Peter Robinett

unread,
Jul 22, 2009, 5:16:51 PM7/22/09
to Lift
Ok, good to know.

For people looking to extend David's code earlier in this thread to
have Comet listeners receive some, and not all, object creation
events, take a look at David's Skittr code[1], namely UserActor[2],
UserList[3], and WatchUser[4]. Basically, a singleton object (UserList
in Skittr's case) stores a HashMap of string keys and actors (e.g.
UserActor) and the Comet listeners (e.g. WatchUser) ask it for the
appropriate actors and register themselves with them. If people want
to know more, I'm willing to do a writeup.

Peter

[1]: http://blog.lostlake.org/index.php?/archives/55-Prance-with-the-Horses,-Skittr-with-the-Mice.html
[2]:
http://github.com/dpp/liftweb/blob/617f752f3824005768e255089f847720b5e67f2a/sites/skittr/src/main/scala/com/skittr/actor/UserActor.scala
[3]:
http://github.com/dpp/liftweb/blob/617f752f3824005768e255089f847720b5e67f2a/sites/skittr/src/main/scala/com/skittr/actor/UserList.scala
[4]:
http://github.com/dpp/liftweb/blob/617f752f3824005768e255089f847720b5e67f2a/sites/skittr/src/main/scala/com/skittr/comet/WatchUser.scala


On Jul 22, 9:30 am, David Pollak <feeder.of.the.be...@gmail.com>
Reply all
Reply to author
Forward
0 new messages