scala - Akka-Http Websockets: How to Send consumers the same stream of data -


i have websocket clients can connect have stream of data using akka-streams. how can make clients same data. @ moment seem racing data.

thanks

one way is have actor extends actorpublisher , have subscribe message.

class mypublisher extends actorpublisher[mydata]{    override def prestart = {     context.system.eventstream.subscribe(self, classof[mydata])   }    override def receive: receive = {      case msg: mydata ⇒       if (isactive && totaldemand > 0) {         // pushes message onto stream         onnext(msg)       }   } }  object mypublisher {   def props(implicit ctx: executioncontext): props = props(new mypublisher()) }  case class mydata(data:string) 

you can use actor source stream:

val datasource = source.actorpublisher[mydata](mypublisher.props(someexcutioncontext)) 

you can create flow datasource , apply transform convert data websocket message

val myflow = flow.fromsinkandsource(sink.ignore, datasource map {d => textmessage.strict(d.data)}) 

then can use flow in route handling.

path("readings") {   handlewebsocketmessages(myflow) }  

from processing of original stream, can publish data event stream , instance of actor pick , put in onto stream websocket being served from.

  val actorsystem = actorsystem("foo")    val othersource = source.fromiterator(()  => list(mydata("a"), mydata("b")).iterator)    othersource.runforeach { msg ⇒ actorsystem.eventstream.publish(mydata("data"))} 

each socket have own instance of actor provide data coming single source.


Comments