A Chat Application in Lift
IEEE Internet Computing (2010)
- ISSN: 10897801
- DOI: 10.1109/MIC.2010.61
Available from portal.acm.org
or
Abstract
The article discusses how to build a multiuser, realtime chat application in Lift and discuss Scala's language features that make Lift possible. The application provides a single chat server that takes chat messages and redistributes the messages out to all listeners. Lift's Comet implementation uses a single HTTP connection to poll for changes to an arbitrary number of components on the page.
Author-supplied keywords
Page 1
A Chat Application in Lift
The Functional Web
88 Published by the IEEE Computer Society 1089-7801/10/$26.00 © 2010 IEEE IEEE INTERNET COMPUTING
L ast year, Debasish Ghosh and Steve Vinoski gave an overview of the Scala language, highlighting some of the features of Scala
using the Lift Web framework in their article,
“Scala and Lift — Functional Recipes for the
Web.”1 We pick up where they left off in this
column by taking a deeper dive into Lift, a Web
framework in the vein of Seaside (www.seaside.
st) and WebObjects (http://developer.apple.com/
tools/webobjects).
In contrast to frameworks oriented around
the model-view-controller (MVC) pattern, Lift
abstracts the HTTP request–response cycle
rather than wrapping HTTP concepts in APIs.
This means you put HTML element definition
and action in the same place:
var name = ""
SHtml.text(name, s => name = s)
This example creates an <input type=
"text" value=""/> tag and associates it with
the function that sets the variable name to the
value the user enters, whether submitted via
Ajax or via a normal HTTP GET or POST. The
advantages of Lift’s approach are numerous,
including increased security through randomly
assigned HTML element names, enhanced
maintainability, and unification between Ajax
and normal HTTP.
Here, we show how to build a multiuser, real-
time chat application in Lift and discuss Scala’s
language features that make Lift possible. The
application provides a single chat server that
takes chat messages and redistributes the mes-
sages out to all listeners. But before we present
the code for the chat application, let’s first briefly
discuss Scala’s support for actors and messages.
Actors and Messages
Similar to Erlang, Scala supports the actor
model, an approach to concurrency in which
each actor is an independent entity capable of
sending and receiving messages to and from
other actors and creating new actors. Whereas
actors are baked into Erlang, they’re a library in
Scala. In fact, Lift has its own actor library that
has different, more Web-friendly performance
characteristics than the Scala actor library. An
actor gives a few guarantees that provide a sim-
ple concurrency model: asynchronous message
sending, processing at most one message at a
time, and processing messages in order from the
actor’s mailbox.
Sending a message to an actor is asynchro-
nous: the message-send method returns almost
immediately. A message send places the message
in the target actor’s mailbox. An actor defines a
set of messages that it can handle at the current
time. When the application causes a message to
be placed into the actor’s mailbox, the actor is
scheduled to review its messages. If the actor
can handle at least one message in its mail-
box, it’s scheduled to execute that message and
potentially other messages in its mailbox. Note
that with actors, you don’t need to lock private
variables because you’re guaranteed to execute
code that can access those variables on only one
thread at a time. Because message sending is
asynchronous, it takes a lot of work to deadlock
an actor.
The Chat Application
To follow along with our chat application
instructions, you’ll need to install on your
computer version 2.2.1 of the Maven project
life-cycle management tool (see http://maven.
apache.org) and Java 1.6. The first thing we’ll
do is create a Lift project’s shell:
mvn archetype:generate \
-DarchetypeGroupId=\
net.liftweb \
-DarchetypeArtifactId=\
A Chat Application in Lift
David Pollak • Lift Web Framework
Steve Vinoski • Verivue
88 Published by the IEEE Computer Society 1089-7801/10/$26.00 © 2010 IEEE IEEE INTERNET COMPUTING
L ast year, Debasish Ghosh and Steve Vinoski gave an overview of the Scala language, highlighting some of the features of Scala
using the Lift Web framework in their article,
“Scala and Lift — Functional Recipes for the
Web.”1 We pick up where they left off in this
column by taking a deeper dive into Lift, a Web
framework in the vein of Seaside (www.seaside.
st) and WebObjects (http://developer.apple.com/
tools/webobjects).
In contrast to frameworks oriented around
the model-view-controller (MVC) pattern, Lift
abstracts the HTTP request–response cycle
rather than wrapping HTTP concepts in APIs.
This means you put HTML element definition
and action in the same place:
var name = ""
SHtml.text(name, s => name = s)
This example creates an <input type=
"text" value=""/> tag and associates it with
the function that sets the variable name to the
value the user enters, whether submitted via
Ajax or via a normal HTTP GET or POST. The
advantages of Lift’s approach are numerous,
including increased security through randomly
assigned HTML element names, enhanced
maintainability, and unification between Ajax
and normal HTTP.
Here, we show how to build a multiuser, real-
time chat application in Lift and discuss Scala’s
language features that make Lift possible. The
application provides a single chat server that
takes chat messages and redistributes the mes-
sages out to all listeners. But before we present
the code for the chat application, let’s first briefly
discuss Scala’s support for actors and messages.
Actors and Messages
Similar to Erlang, Scala supports the actor
model, an approach to concurrency in which
each actor is an independent entity capable of
sending and receiving messages to and from
other actors and creating new actors. Whereas
actors are baked into Erlang, they’re a library in
Scala. In fact, Lift has its own actor library that
has different, more Web-friendly performance
characteristics than the Scala actor library. An
actor gives a few guarantees that provide a sim-
ple concurrency model: asynchronous message
sending, processing at most one message at a
time, and processing messages in order from the
actor’s mailbox.
Sending a message to an actor is asynchro-
nous: the message-send method returns almost
immediately. A message send places the message
in the target actor’s mailbox. An actor defines a
set of messages that it can handle at the current
time. When the application causes a message to
be placed into the actor’s mailbox, the actor is
scheduled to review its messages. If the actor
can handle at least one message in its mail-
box, it’s scheduled to execute that message and
potentially other messages in its mailbox. Note
that with actors, you don’t need to lock private
variables because you’re guaranteed to execute
code that can access those variables on only one
thread at a time. Because message sending is
asynchronous, it takes a lot of work to deadlock
an actor.
The Chat Application
To follow along with our chat application
instructions, you’ll need to install on your
computer version 2.2.1 of the Maven project
life-cycle management tool (see http://maven.
apache.org) and Java 1.6. The first thing we’ll
do is create a Lift project’s shell:
mvn archetype:generate \
-DarchetypeGroupId=\
net.liftweb \
-DarchetypeArtifactId=\
A Chat Application in Lift
David Pollak • Lift Web Framework
Steve Vinoski • Verivue
Sign up today - FREE
Mendeley saves you time finding and organizing research. Learn more
- All your research in one place
- Add and import papers easily
- Access it anywhere, anytime
Start using Mendeley in seconds!
Readership Statistics
3 Readers on Mendeley
by Discipline
by Academic Status
67% Other Professional
33% Associate Professor
by Country
67% United States
33% Belgium


