http4s / http4s

A minimal, idiomatic Scala interface for HTTP
https://http4s.org/
Apache License 2.0
2.56k stars 789 forks source link

Server Sent Events #619

Open rossabaker opened 8 years ago

rossabaker commented 8 years ago
rossabaker commented 8 years ago

We don't currently a decoder to a Process. We can decode the entity to a Task[Process[Task,_]], but, eh?

agourlay commented 7 years ago

I am really interested in receiving ServerSentEvent on the client side. My use case is to connect to an SSE endpoint an gather all events for a given duration. What is missing to achieve that? I am willing to offer help to push this forward 😃

rossabaker commented 7 years ago

First off, since the above comment is old, Process is Stream and Task[Process[Task, _]] is F[Stream[F, _]]. But there still exists no EntityDecoder to a Stream. And maybe there doesn't need to be: we can probably build on response.body.through(ServerSentEvent.decoder) to get our Stream[F, ServerSentEvent]. At a simple level, that might just work.

At a more complete level, it's worth looking at the Processing model. Some servers might be strict in checking things like the Accept header. Maybe a def streamServerSentEvents(uri: Uri, lastEventId: Option[LastEventId]): Stream[F, ServerSentEvent] method on the client that set all those headers and checked status codes appropriately would be a good idea?

kalexmills commented 4 years ago

I'd like to use comment lines to provide a keep-alive notification and keep the browser from shutting down existing streams.

Per the spec lines starting with : are 'comments' and are ignored by the client, but should serve to keep the socket open. This is even recommended in the spec here

Are folks open to adding functionality for sending comments to the implementation? My alternative right now is to send empty data, which is just 5 bytes too many. ;-)

But more importantly it could cause confusion for the client to receive empty 'data' packets. The comments are explicitly ignored by the underlying implementation, so it should fit this use-case gracefully.

EDIT: Also it looks like the decoder is in the code-base. This might be done. Lemme know if I should open a new issue or a PR.

rossabaker commented 3 years ago

Setting aside the comments issue we overlooked (:disappointed:), I think the client would be neat. It's probably too esoteric for the main Client interface, but can't go on core. A new SseClient that wraps a Client would be a good home for implementing this spec.