Reactive Web Applications using Scala & Liftweb Andrea Zanda CTO & Co-founder andrea.zanda@guidemeright.com Riccardo Sirigu Full Stack Dev riccardo.sirigu@guidemeright.com
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
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 Guest
www.guidemeright.com • +1.5K Activities Where are we now? • 15 Italian Regions • +300 active Local Friend • +500 Experiences Booked • € 200K of Seed Money
Which is the tech challenge?
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 structures scala> val even = List(2,4) scala> 6 :: 8 :: even res0: List[Int] = List(6,8,2,4) ● Type inference scala> 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” :: Nil names: List[String] = List(Riccardo, Andrea, Mario) names.map(botMe) res0: List[String] = List(@RiccardoBot, @AndreaBot, @MarioBot)
Objects Everywhere Functions are Objects (x: Int) => x * x * x ..is expanded to.. new Function1[Int, Int] { def apply(x: Int) = x * x * x }
Objects Everywhere val f = (x: Int) => x * x * x f(2) val f = new Function1[Int, Int] { def apply(x: Int) = x * x * x } f.apply(2)
Companion Objects
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
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 ! msg Actor Model
LIFT me up
Introducing Lift Why? ● 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
Setting the environment +
MVVM in concrete View Model HTML page Scala Class (ORM) Snippet (MV)
CSS Selectors “#id_element” #> <p> Element Content</p>
SHtml “#id_text” #> SHtml.text(“text”, textVar = _) & “#id_submit” #> SHtml.submit(“Submit”, processResult()) def processResult(): JsCmd = { //do something S.notice(“Work Done”) }
Comet Actors
Comet • Server Push • Real time event-based systems • Thread per request, long polling
Comet One thread per request leads to…….. SCALABILITY ISSUES Web container request suspension or continuation
Demo Time
follow up: andrea.zanda@guidemeright.com riccardo.sirigu@guidemeright.com
DONA ANCHE TU su Rete del Dono #sardiniacodeweek http://www.retedeldono.it/it/progetti/sardegna- 2050/sardinia-code-week-2015

Reactive Web Applications with Scala & Liftweb - CodeWeek 2015

  • 1.
    Reactive Web Applications usingScala & Liftweb Andrea Zanda CTO & Co-founder andrea.zanda@guidemeright.com Riccardo Sirigu Full Stack Dev riccardo.sirigu@guidemeright.com
  • 2.
    DONA ANCHE TUsu Rete del Dono #sardiniacodeweek http://www.retedeldono.it/it/progetti/sardegna- 2050/sardinia-code-week-2015
  • 3.
    www.guidemeright.com Current touristic offerlacks of authenticity There is a whole heritage of local and authentic socio- cultural activities and experiences Opportunity
  • 4.
  • 5.
    www.guidemeright.com LOCAL FRIENDGUEST A CommunityMarketplace 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
  • 6.
  • 7.
    www.guidemeright.com • +1.5K Activities Whereare we now? • 15 Italian Regions • +300 active Local Friend • +500 Experiences Booked • € 200K of Seed Money
  • 8.
    Which is thetech challenge?
  • 9.
    Reactive Manifesto ● Thetechniques 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”
  • 10.
    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
  • 11.
  • 14.
    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
  • 15.
    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
  • 16.
    Scala vs Java ●Immutable data structures scala> val even = List(2,4) scala> 6 :: 8 :: even res0: List[Int] = List(6,8,2,4) ● Type inference scala> Map('a' -> 1, 'b' -> 2) res2: scala.collection.immutable.Map[Char,Int] = Map(a -> 1, b -> 2)
  • 17.
    Scala vs Java classUser(val email: String) class User{ private String email; public User(String email){ this.email = email } public String getEmail(){ return email; } }
  • 18.
    Scala (1st class)Functions var botMe = (name: String) => “@” + name + ”Bot” botMe: String => String = <function1> val names = “Riccardo” :: “Andrea” :: “Mario” :: Nil names: List[String] = List(Riccardo, Andrea, Mario) names.map(botMe) res0: List[String] = List(@RiccardoBot, @AndreaBot, @MarioBot)
  • 19.
    Objects Everywhere Functions areObjects (x: Int) => x * x * x ..is expanded to.. new Function1[Int, Int] { def apply(x: Int) = x * x * x }
  • 20.
    Objects Everywhere val f= (x: Int) => x * x * x f(2) val f = new Function1[Int, Int] { def apply(x: Int) = x * x * x } f.apply(2)
  • 21.
  • 22.
    Companion Objects • Samename & 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" }
  • 23.
    Pattern Matching • Switchcase on steroids User.findById(“000001”) match { case Some(user) => println(user.name) case Empty => println(“No User Found!”) }
  • 24.
  • 25.
    Scala Actors ● Manualthreading and locking is difficult Actor abstraction ● Concurrent processes that communicate by exchanging messages ● Actor encapsulates both state and behavior ● Fire&Forget chatserver ! msg Actor Model
  • 26.
  • 27.
    Introducing Lift Why? ● Basedon 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
  • 29.
  • 30.
    MVVM in concrete ViewModel HTML page Scala Class (ORM) Snippet (MV)
  • 31.
    CSS Selectors “#id_element” #><p> Element Content</p>
  • 32.
    SHtml “#id_text” #> SHtml.text(“text”,textVar = _) & “#id_submit” #> SHtml.submit(“Submit”, processResult()) def processResult(): JsCmd = { //do something S.notice(“Work Done”) }
  • 33.
  • 34.
    Comet • Server Push •Real time event-based systems • Thread per request, long polling
  • 35.
    Comet One thread perrequest leads to…….. SCALABILITY ISSUES Web container request suspension or continuation
  • 36.
  • 37.
  • 38.
    DONA ANCHE TUsu Rete del Dono #sardiniacodeweek http://www.retedeldono.it/it/progetti/sardegna- 2050/sardinia-code-week-2015