playframework / play-socket.io

Play socket.io support
Apache License 2.0
92 stars 33 forks source link

Check if session is not live #8

Open omidb opened 6 years ago

omidb commented 6 years ago

Let's say we have a session id, how can I check if that session is alive or already has been closed?

jroper commented 6 years ago

Play socket.io offers a stream centric view of socket.io - if the stream is running, the session is alive, if it's not, it's been closed. If you want to maintain that state somewhere, you could use watchTermination on the stream to update a map somewhere of active stream ids, but note that that won't work in a multi node environment, Play socket.io is designed fundamentally to support multi node environments. You could maintain the map in a cluster singleton actor.

omidb commented 6 years ago

Currently what I am doing is very similar to what you mentioned, I use onStop and onStart to keep track of them:

class SocketIOEngine(socketIO: SocketIO , system: ActorSystem, subscriberActor: ActorRef @@ SubscriberActor,
                     myLagomService: MyLagomService)(implicit mat: Materializer) {

  val decoder = decodeByName {
    ....
  }

  val encoder = encodeByType {
    ....
  }

  def flow(id:String) = {
    implicit val timeout = Timeout(1.second)
    ActorFlow.actorRef { out =>
      DashboardActor.props(id, out, subscriberActor, lagomService)
    }(system, mat)
  }

  val controller: EngineIOController = socketIO.builder
    .addNamespace(decoder, encoder){
      case (SocketIOSession(sessionId, out),"/chat") => {
        flow(sessionId)
      }
    }
    .createController()
}

I don't know how well it scales, becasue there is only one actor that keeps track of everything. On the other hand, I am subscribing to bunch of events coming from Lagom Kafka topics in the same actor and send them to the front end actors. Also, (not related to the Play Socket but more generally related to Lagom and Play connections) it gets ugly to do encoding and decoding of all these messages and convert them around. Is there any way to wire these nicer? I was thinking, if we could have the sockets in Play like what we have in Lagom services (two Sources), maybe then we can wire them around without all these conversion layers.