How to do it...

  1. To begin with, create a file named ChatServer.scala with the following contents:
        package com.packt.chapter7        import akka.actor.{Actor, ActorRef, Props, Terminated}        object ChatServer {          case object Connect          case object Disconnect          case object Disconnected          case class Message(author: ActorRef, body: String,          creationTimestamp : Long = System.currentTimeMillis())          def props = Props(new ChatServer())        }        class ChatServer extends Actor {          import ChatServer._          var onlineClients = Set.empty[ActorRef]          def receive = {            case Connect =>              onlineClients += sender              context.watch(sender)            case Disconnect =>              onlineClients -= sender              context.unwatch(sender)              sender ! Disconnected            case Terminated(ref) =>              onlineClients -= ref            case msg: Message => onlineClients.filter(_ ...

Get Akka Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.