Friday 26 March 2010

Fixing Michael Galpin's Comet Tutorial

I recently stumbled across the following tutorial on Comet:

Build Comet applications using Scala, Lift, and jQuery
http://www.ibm.com/developerworks/ajax/tutorials/wa-aj-comet/index.html

I was interested in this tutorial as comet is an alternative architecture to Ajax. In Ajax the browser makes calls (polls) to the web server in the background and then updates the users web page. This issue with this approach is that there is a delay between a change and the polling call. Also when you have a large number of users there will be a lot of polling calls hitting the web server that will not update the users web page (as there was no change).

Comet takes an alternative approach where it keeps a connection open to the user's browser and uses this to push data to the use. The benefit of this approach is that it essentially uses the Observer Pattern an only has web traffic when an update has been made which reduces load on the web server and is more scalable.

a potential replacement for Comet is Web Sockets in HTML 5 but this isn't supported by all browsers at present.

Anyway, when I tried building the tutorial I found that Michael had used a snapshot build to get new features but as this is a snapshot it is removed later from the Maven repositories giving this error:

[INFO] Failed to resolve artifact.

Missing:
----------
1) net.liftweb:lift-core:jar:0.10-SNAPSHOT


This is easily fixed by moving to the released version by changing pom.xml from:

net.liftweb
lift-core
0.10-SNAPSHOT


to the release version:

net.liftweb
lift-core
0.10


But unfortunately there were changes between the snapshot and the release which threw the following errors:

[INFO] Compiling 7 source files to C:\dev\scala\lift\broken_auctionNet\target\cl
asses at 1269595709118
[ERROR] C:\dev\scala\lift\broken_auctionNet\src\main\scala\bootstrap\liftweb\Boo
t.scala:44: error: value appendEarly is not a member of object net.liftweb.http.
LiftRules
[INFO] LiftRules.appendEarly(makeUtf8)
[INFO] ^
[ERROR] C:\dev\scala\lift\broken_auctionNet\src\main\scala\bootstrap\liftweb\Boo
t.scala:66: error: not found: type Can
[INFO] private def createOne: Can[Connection] = try {
[INFO] ^
[ERROR] C:\dev\scala\lift\broken_auctionNet\src\main\scala\bootstrap\liftweb\Boo
t.scala:87: error: not found: type Can
[INFO] def newConnection(name: ConnectionIdentifier): Can[Connection] =
[INFO] ^
[ERROR] C:\dev\scala\lift\broken_auctionNet\src\main\scala\org\developerworks\co
met\AuctionActor.scala:18: error: type mismatch;
[INFO] found : java.lang.String("auction")
[INFO] required: net.liftweb.util.Box[String]
[INFO] def defaultPrefix = "auction"
[INFO] ^
[ERROR] four errors found

The Can errors are fixed by replacing Can with Box in the two places in boot.scala

To fix the final error replace:
def defaultPrefix = "auction"

by boxing it Full and make it an override as follows:
override def defaultPrefix = Full("auction")

This fixes the errors but when you compile you get the following error in boot.scala:

[INFO] Compiling 7 source files to C:\dev\scala\lift\broken_auctionNet\target\cl
asses at 1269597516624
[ERROR] C:\dev\scala\lift\broken_auctionNet\src\main\scala\bootstrap\liftweb\Boo
t.scala:44: error: value appendEarly is not a member of object net.liftweb.http.
LiftRules
[INFO] LiftRules.appendEarly(makeUtf8)
[INFO] ^
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR

This is fixed by changing the line from:
LiftRules.appendEarly(makeUtf8)

to:
LiftRules.early.append(makeUtf8)

The code now compiles but when you start it using mvn jetty:run you get the following

2010-03-26 10:06:39.757:WARN::failed org.mortbay.jetty.plugin.Jetty6PluginWebApp
Context@33c78b{/,C:\dev\scala\lift\broken_auctionNet\src\main\webapp}: java.lang
.VerifyError: (class: net/liftweb/util/EmptyBox, method: open_$bang signature: (
)Ljava/lang/Object;) Can only throw Throwable objects
2010-03-26 10:06:39.757:WARN::failed ContextHandlerCollection@171bc3f: java.lang
.VerifyError: (class: net/liftweb/util/EmptyBox, method: open_$bang signature: (
)Ljava/lang/Object;) Can only throw Throwable objects
2010-03-26 10:06:39.757:WARN::failed HandlerCollection@1fddb33: java.lang.Verify
Error: (class: net/liftweb/util/EmptyBox, method: open_$bang signature: ()Ljava/
lang/Object;) Can only throw Throwable objects
2010-03-26 10:06:39.757:WARN::Error starting handlers
java.lang.VerifyError: (class: net/liftweb/util/EmptyBox, method: open_$bang sig
nature: ()Ljava/lang/Object;) Can only throw Throwable objects
at net.liftweb.http.LiftRules$.(LiftRules.scala:171)

This is resolved by increasing the Scala version to 2.7.3 from:

2.7.2

to:

2.7.3