reactive web applications with scala & liftweb - codeweek 2015
TRANSCRIPT
Reactive Web Applications using Scala & Liftweb
Andrea ZandaCTO & [email protected]
Riccardo SiriguFull Stack [email protected]
DONA ANCHE TU su Rete del Dono#sardiniacodeweek
http://www.retedeldono.it/it/progetti/sardegna-2050/sardinia-code-week-2015
www.guidemeright.com
Current touristic offer lacks of authenticity
There is a whole heritage of local and authentic socio-cultural activities and experiences
Opportunity
www.guidemeright.com
LOCAL FRIENDGUEST
A Community Marketplace where to discover and book authentic local experiences with a Local Friend, a local person who can earn by
sharing his local knowledge and lifestyle
www.guidemeright.com
• +1.5K Activities
Where are we now?
• 15 Italian Regions
• +300 active Local Friend
• +500 Experiences Booked
• € 200K of Seed Money
Reactive Manifesto
● The techniques and technologies traditionally used in a class of Reactive and Interactive Systems is gaining quite a bit of attention from the mainstream developers community
● Recently, the Reactive Manifesto was defined to summarise the key traits of “Reactive Applications”
Reactive Manifesto
● Responsive: Timely react to stimuli
● Event-Driven: modular, asynchronous
● Resilent: Tolerate failures functionally and ideally temporally
● Scalable: Gracefully scale up and down depending on load demands
https://github.com/zaza81/lift-codeweek
@MarioBot /codeweek banana luca
gmr-codeweek.cloudapp.net
Scala● Functional (style) programming● Everything is an Object● Run on JVM● Practical expressivity
○ Expressive Power
○ List(1,2,3,4).map(x => x + 1)○ What vs How
● N(Bugs) ∝ N(LOC)○ Fewer LOC fewer bugs○ Halstead complexity measures
Scala vs Java● Every operator is a method call○ 12 + 4 (12).+(4)○ “#name” #> “david”○ “codeWeek” indexOf “w”
● Object Equality○ List(56,13,75) == List(56,13,75)
● Optional Types○ NullPointerException○ val secret: Option[String] = Some(“zbmdfbmknnaprzvds=”)○ val secret: Option[String] = None
Scala vs Java● Immutable data structuresscala> val even = List(2,4)scala> 6 :: 8 :: evenres0: List[Int] = List(6,8,2,4)
● Type inferencescala> Map('a' -> 1, 'b' -> 2)res2: scala.collection.immutable.Map[Char,Int] = Map(a -> 1, b -> 2)
Scala vs Java
class User(val email: String)
class User{ private String email;
public User(String email){this.email = email
}public String getEmail(){return email;
}}
Scala (1st class) Functions
var botMe = (name: String) => “@” + name + ”Bot”botMe: String => String = <function1>
val names = “Riccardo” :: “Andrea” :: “Mario” :: Nilnames: List[String] = List(Riccardo, Andrea, Mario)
names.map(botMe)res0: List[String] = List(@RiccardoBot, @AndreaBot, @MarioBot)
Objects EverywhereFunctions are Objects
(x: Int) => x * x * x
..is expanded to..
new Function1[Int, Int] { def apply(x: Int) = x * x * x
}
Objects Everywhereval f = (x: Int) => x * x * xf(2)
val f = new Function1[Int, Int] { def apply(x: Int) = x * x * x}
f.apply(2)
Companion Objects• Same name & same source file as the class
class User extends MegaProtoUser[User] { def getSingleton = User // reference to the companion object below def allAccounts : List[Account] = Account.findAll(By(Account.owner, this.id))}// Create a "companion object" to the User class (above).// The companion object is a "singleton" object that shares the same// name as its companion class.object User extends User with MetaMegaProtoUser[User] { override def dbTableName = "users" }
Pattern Matching
• Switch case on steroids
User.findById(“000001”) match {case Some(user) => println(user.name)case Empty => println(“No User Found!”)
}
Scala Actors● Manual threading and locking is difficult Actor abstraction● Concurrent processes that communicate by exchanging messages● Actor encapsulates both state and behavior● Fire&Forget
chatserver ! msgActor Model
Introducing LiftWhy?
● Based on Scala● Comet and Ajax, js abstractions● Designer friendly templates View First● Security
○ injections, XSS, CSRF...○ OWASP Top 10
http://seventhings.liftweb.net/http://lift.la/blog/lift-state-and-scaling
SHtml
“#id_text” #> SHtml.text(“text”, textVar = _) & “#id_submit” #> SHtml.submit(“Submit”, processResult())
def processResult(): JsCmd = { //do something
S.notice(“Work Done”)}
Comet
One thread per request leads to…….. SCALABILITY ISSUES
Web container request suspension or continuation
follow up:
[email protected]@guidemeright.com