CometActor

From LiftWiki

Jump to: navigation, search

Comet is supported in Lift via the use of a CometActor.

A CometActor is an Actor that sends messages to the browser. If you want to change the state of the CometActor, you send it messages as you would any other Actor.


To use a CometActor, you embed a call to it in your template:

<lift:comet type="CometFoo">
<a:b>Default Text</a:b>
</lift:comet>

Then in your comet sub-package (say, com.example.comet), you create your subclass of CometActor:

package com.example.comet

import scala.actors.Actor._  

import net.liftweb.http._    

class CometFoo(info: CometActorInitInfo) extends CometActor(info) {  
  val defaultPrefix = "a"  
  var message = "hello!"
  
  def render = bind("b" -> <span>{message}</span>) 
 
  override def localSetup = {  
    // If you want to setup some initial state, this is the place.  
  }  
  
  override def localShutdown = {  
    // Here is where you will teardown any state when this CometActor is no longer in use.  
  }  
    
  override def lowPriority : PartialFunction[Any, Unit] = {  
    case UpdateMessage(msg) => message = msg; reRender(false)  
  }  
}

case class UpdateMessage(msg: String) 

A few points:

The <a:b> in your template is defined as defaultPrefix:bind's first argument.

lowPriority is what you implement to accept messages sent to your CometActor. There is also mediumPriority and highPriority.


Jorge Ortiz wrote a great blog post that uses CometActors.

Personal tools
search|search