{"action":"opened","issue":{"url":"https://api.github.com/repos/playframework/play-streaming-scala/issues/11","repository_url":"https://api.github.com/repos/playframework/play-streaming-scala","labels_url":"https://api.github.com/repos/playframework/play-streaming-scala/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/playframework/play-streaming-scala/issues/11/comments","events_url":"https://api.github.com/repos/playframework/play-streaming-scala/issues/11/events","html_url":"https://github.com/playframework/play-streaming-scala/issues/11","id":193385567,"number":11,"title":"Could you add a SSE example that uses `Source.actorRef` ?","user":{"login":"rtyley","id":52038,"avatar_url":"https://avatars.githubusercontent.com/u/52038?v=3","gravatar_id":"","url":"https://api.github.com/users/rtyley","html_url":"https://github.com/rtyley","followers_url":"https://api.github.com/users/rtyley/followers","following_url":"https://api.github.com/users/rtyley/following{/other_user}","gists_url":"https://api.github.com/users/rtyley/gists{/gist_id}","starred_url":"https://api.github.com/users/rtyley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rtyley/subscriptions","organizations_url":"https://api.github.com/users/rtyley/orgs","repos_url":"https://api.github.com/users/rtyley/repos","events_url":"https://api.github.com/users/rtyley/events{/privacy}","received_events_url":"https://api.github.com/users/rtyley/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-04T23:45:36Z","updated_at":"2016-12-04T23:45:36Z","closed_at":null,"body":"I'm experimenting with Server Sent Events, and while the existing `streamClock()` example is nice, I've failed in adapting it to my purpose. I'd like:\r\n\r\n* a 'writable' `Source[String, _]` that I can push data into arbitrarily at a later date. Is that what `Source.actorRef()` is for?\r\n* a `Publisher` for which `fanout` is true (ie `Sink.asPublisher(fanout = true)`), so that multiple clients can all receive the resulting server-sent events.\r\n\r\nI hope those are reasonable things to want, and that server-sent events are a reasonable way to achieve it. You can imagine a site with a big red button, and another 'viewer' page which many people could view - when the button is pressed, all users should see that event relayed on the 'viewer' page, simultaneously.\r\n\r\nHere's the kind of code I've been trying:\r\n\r\n```\r\n  val (actorRef, publisher) =\r\n    Source.actorRef[String](1000, OverflowStrategy.dropHead).toMat(Sink.asPublisher(fanout = true))(Keep.both).run()\r\n\r\n  val sourceFromPublisher = Source.fromPublisher(publisher).log(\"actorPublisher\")\r\n\r\n  def streamClock() = Action {\r\n    Ok.chunked(sourceFromPublisher via EventSource.flow).as(ContentTypes.EVENT_STREAM)\r\n  }\r\n```\r\n_(see https://github.com/rtyley/play-streaming-scala/commit/77572a8c69ff1181120af211897a0bf4958f99c2#diff-0558d2a6b4820ecb3797f61ab919efb4 for this code in a compiling branch)\r\n\r\nWhen I try this, I sometimes get success - everything works as intended, but more often than not, I get this error:\r\n\r\n```\r\n[error] a.s.Materializer - [actorPublisher] Upstream failed.\r\nakka.stream.impl.ActorPublisher$NormalShutdownException: Cannot subscribe to shut-down Publisher\r\n```\r\n\r\n...the intermittent nature of the error suggests to me that there's some kind of race-condition going on, with the publisher often getting shut-down before it can be registered for use. What's the proper way to use this stuff?!\r\n\r\nhttps://www.playframework.com/documentation/2.5.x/ScalaComet\r\n\r\ncc @wsargent "}}
{"actor":{"display_login":"rtyley"}}