Book Image

Building Applications with Scala

By : Diego Pacheco
Book Image

Building Applications with Scala

By: Diego Pacheco

Overview of this book

<p>Scala is known for incorporating both object-oriented and functional programming into a concise and extremely powerful package. However, creating an app in Scala can get a little tricky because of the complexity the language has. This book will help you dive straight into app development by creating a real, reactive, and functional application. We will provide you with practical examples and instructions using a hands-on approach that will give you a firm grounding in reactive functional principles.</p> <p>The book will take you through all the fundamentals of app development within Scala as you build an application piece by piece. We’ve made sure to incorporate everything you need from setting up to building reports and scaling architecture. This book also covers the most useful tools available in the Scala ecosystem, such as Slick, Play, and Akka, and a whole lot more. It will help you unlock the secrets of building your own up-to-date Scala application while maximizing performance and scalability.</p>
Table of Contents (17 chapters)
Building Applications with Scala
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface

The chat protocol


Now we will need to define our protocol. For this functionality, we will need three Actors. The Actors that we create will be as follows:

  • ChatRoom: This will have a reference for all users in the chat room

  • ChatUser: This will have one instance per user (active browser)

  • ChatBotAdmin: This simple Bot Admin will provide stats about the chat room

ChatUserActor will need to join JoinChatRoom object in order to start chatting. ChatUserActor will also need to send messages to ChatMessage class to the ChatRoomActor that will broadcast messages to all users. The ChatBotAdmin will get a report from  GetStats object from ChatRoomActor.

Let's start coding this protocol. First, we will need to define the messages that will be exchanged between these Actors, as shown in the following piece of code:

    package actors  
    case class ChatMessage(name:String,text: String) 
    case class Stats(users:Set[String])  
    object JoinChatRoom 
    object Tick 
    object...