Comet best practices?

144 views
Skip to first unread message

Philip Davis

unread,
May 16, 2012, 7:02:42 PM5/16/12
to lif...@googlegroups.com
Hello, I'm quite familiar with snippets and memoize... but I am only starting to get into comet and I'm wondering what best practices have been established (outside of chat apps :)

I'd like to have a page of things, say buttons for simplicity, that the user can interact with.  Each user interaction will kick off some lengthy processing on the server.  So ideally the server would send an update once processing is complete.  This update would redraw/change the 'button' (but just that button... I don't want to refresh the entire list).  The number of buttons will be different from page to page, but won't change dynamically on any given page.

I'm not clear on how the snippet interacts with the comet actor.  Specifically, 
1) Should I memoize each button in the snippet -- and then if so, how does the comet actor get at the memoized copy to reapply the transformation?
2) Or should the comet actor be responsible for rendering all of the buttons initially so that it can update the correct one?
3) is there something else I'm overlooking?

Thanks for your time.  After re-reading, it sounds like 2 is the obvious answer.  But I'd appreciate expert input just in case!

Diego Medina

unread,
May 16, 2012, 11:02:05 PM5/16/12
to lif...@googlegroups.com
Hi Philip,

You are correct, the option number 2 is the right way to go about it.
This blog post may help you get started
http://blog.fmpwizard.com/54204619
it shows how you can have a form, after you submitted, a process gets
kicked off, which takes a while, so the user can go to other pages, or
wait there, and once the job is done, the comet actor updates the UI
to notify the user that the job is done. In your case it could send
some JavaScript to modify the button.

Hope that helps.

And this links shows you some other posts I have written that cover LiftComets
http://blog.fmpwizard.com/tag/comet

Regards,

Diego
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you:
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Philip Davis

unread,
May 17, 2012, 12:21:03 PM5/17/12
to lif...@googlegroups.com
Thanks, Diego.  I had already read some of your posts and code on comet.  It's super helpful to have more experienced users like yourself document their work.

However, I haven't been able to find anything yet that addresses my needs (or I'm failing to see a connection).  In one of your examples you're simply replacing an element with a new string.  In another example you're replacing the contents of a cell with a <td> element generated inline in Scala.

I think the crux of my problem is that I don't want to generate elements inline in Scala.  I basically want this:

  override def mediumPriority = {
    case LongOperation(thing) =>
      Thread.sleep(2000)
      partialUpdate(JsCmds.Replace(thing.id, MEMOIZED_THING.applyAgain))
  }

But I don't know what is the best way to achieve this.  Every idea I come up with looks like an ugly hack... just hoping there's a clean solution out there.

David Pollak

unread,
May 17, 2012, 1:07:20 PM5/17/12
to lif...@googlegroups.com
On Wed, May 16, 2012 at 4:02 PM, Philip Davis <mrphili...@gmail.com> wrote:
Hello, I'm quite familiar with snippets and memoize... but I am only starting to get into comet and I'm wondering what best practices have been established (outside of chat apps :)

I'd like to have a page of things, say buttons for simplicity, that the user can interact with.  Each user interaction will kick off some lengthy processing on the server.  So ideally the server would send an update once processing is complete.  This update would redraw/change the 'button' (but just that button... I don't want to refresh the entire list).  The number of buttons will be different from page to page, but won't change dynamically on any given page.

I'm not clear on how the snippet interacts with the comet actor.  Specifically, 
1) Should I memoize each button in the snippet -- and then if so, how does the comet actor get at the memoized copy to reapply the transformation?
2) Or should the comet actor be responsible for rendering all of the buttons initially so that it can update the correct one?
3) is there something else I'm overlooking?


In general, if you are changing markup from the CometActor, then the CometActor should be responsible for rendering all the markup and the controls in the markup. (I am not discussing a CometActor that is responsible for changing data structures on the page and then calling a function to update the display based on the changed data).

CometActor has a partialUpdate(update: JsCmd) method.  When there's a partial update (not a re-rendering of the entire component), you send the JsCmd that will perform the update.  idMemoize has the setHtml method which returns a JsCmd, so you can do a partialUpdate with idMemoize's setHtml and you'll be golden.
 
Thanks for your time.  After re-reading, it sounds like 2 is the obvious answer.  But I'd appreciate expert input just in case!

--



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


Philip Davis

unread,
May 17, 2012, 1:46:11 PM5/17/12
to lif...@googlegroups.com
In general, if you are changing markup from the CometActor, then the CometActor should be responsible for rendering all the markup and the controls in the markup. (I am not discussing a CometActor that is responsible for changing data structures on the page and then calling a function to update the display based on the changed data).

CometActor has a partialUpdate(update: JsCmd) method.  When there's a partial update (not a re-rendering of the entire component), you send the JsCmd that will perform the update.  idMemoize has the setHtml method which returns a JsCmd, so you can do a partialUpdate with idMemoize's setHtml and you'll be golden.

Thanks, David.

If I understand correctly, it sounds like you're saying I should be able to do something like this:

  override def mediumPriority = {
    case LongOperation(thing, id_memoize_transform) =>
      Thread.sleep(2000)
      partialUpdate(id_memoize_transform.setHtml)
  }
 
And set up my render function to call idMemoize for each item in the list, passing each IdMemoizeTransform parameter into a closure that sends a message to my handler.
Reply all
Reply to author
Forward
0 new messages