Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adding support for SSE events to autosource controllers #15

Open
bjartek opened this issue Oct 10, 2013 · 0 comments
Open

[Feature] Adding support for SSE events to autosource controllers #15

bjartek opened this issue Oct 10, 2013 · 0 comments

Comments

@bjartek
Copy link
Contributor

bjartek commented Oct 10, 2013

I am currently playing with a small project where I do some CRUD operations and send events via SSE to clients when operations occur.

 val (enumerator, channel) = Concurrent.broadcast[JsValue]

  def streamUpdates = Action {
    Ok.stream(enumerator &> EventSource()).as("text/event-stream")
  }

And exposing streamUpdates at say /GET //events is all that is needed. (Not sure what to do it the auto source controller is typed.

Then in the methods that do actions that you want to push you can do:

 def update(id: BSONObjectID): EssentialAction = Action(parse.json){ request =>

    Json.fromJson[JsObject](request.body)(reader).map{ t =>
      Async{
        res.update(id, t).map{ _ =>
          val event = JsObject(List("type" -> JsString("update"), "data" -> t))
          channel.push(event)
          Ok(Json.toJson(id)(idWriter))
        }
      }
    }.recoverTotal{ e => BadRequest(JsError.toFlatJson(e)) }
  }

In JS you can then do: http://www.html5rocks.com/en/tutorials/eventsource/basics/ to react on updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant