ScalaLoci

Step-by-Step Example

The example demonstrates the design of a chat application in five steps

  1. Declaring a Server
  2. Declaring a Client
  3. Declaring a message event on the Client and firing the event for every user input
  4. Declaring a publicMessage event on the Server aggregating all events from the clients
  5. Declaring a client-side observer for the server-side publicMessage event
@multitier object Chat {
  trait Server extends Peer { type Tie <: Multiple[Client] }













}
A single server
Server declaration


  trait Client extends Peer { type Tie <: Single[Server] }













Two clients
Client declaration




  val message = placed[Client] { Evt[String]() }





  placed[Client].main {

    for (line <- io.Source.stdin.getLines)
      message fire line
  }

A message event, placed on every client
Event message declaration






  val publicMessage = placed[Server] {
    message.asLocalFromAllSeq map { case (_, message) => message }
  }







Messages are sent from the clients' message events to the publicMessage event on the server
Event publicMessage declaration











    publicMessage.asLocal observe println




Messages are forwarded from the publicMessage event on the server to all clients, which print them to their console
Remote observer for publicMessage declaration
Incremental Chat example