title: Scala, Play, Akka, Websocket: how to pass actor messages through websocket
body: <p>I have an actor which is launched with application, running in the background watching for certain changes and if there are any reporting them. At the moment it just a <code>println</code> to the console. What I need to do is whenever there is a new message - send it to the front end using Websocket.</p>
  
  <p>This is my Play Global object where the monitoring/listening actor is launched:</p>
  
  <pre><code>object Global extends GlobalSettings {
  
      override def onStart(app: Application) {
  
          class Listener extends Actor {
              //This needs to be changed to pass messages to Websocket, how?
              def receive = {
                  case Create(path) =&gt; println("CREATE " + path)
                  case Delete(path) =&gt; println("DELETE " + path)
                  case Modify(path) =&gt; println("MODIFY " + path)
              }
          }
  
          val listener = Akka.system.actorOf(Props[Listener], "listener")
          val swatch = Akka.system.actorOf(Props[SwatchActor], "swatch")
          swatch ! Watch("/folder/path", Seq(Create, Modify, Delete), true, Some(listener))
  
      }
  
  }
  </code></pre>
  
  <p>This is my Play controller:</p>
  
  <pre><code>object Application extends Controller {
  
      def test = WebSocket.using[String] { request =&gt;
  
          //This hopefully gets the listener actor reference?
          val listener = Akka.system.actorSelection("/user/listener")
  
          val (out, channel) = Concurrent.broadcast[String]
          val in = Iteratee.foreach[String] { msg =&gt;
              //Actor messages must be pushed here, how?
              channel push("RESPONSE: " + msg)
          }
  
          (in, out)
  
      }   
  
  }
  </code></pre>
  
  <p>I understand that in order for websocket connection to be established there has to be an initial "in".</p>
  
  <p>So my problems are:</p>
  
  <ol>
  <li>How do I modify the <code>Listener</code> actor to push messages to Websocket?</li>
  <li>What do I need to do to prepare the actor to push messages once the websocket connection is established?</li>
  <li>How do I push messages from the listener actor to the websocket?</li>
  </ol>