| Copyright | (c) 2024 Shaun Sharples |
|---|---|
| License | BSD3 |
| Stability | alpha |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Servant.API.EventStream
Description
Synopsis
- data ServerEvent = ServerEvent {
- eventType :: !(Maybe ByteString)
- eventId :: !(Maybe ByteString)
- eventData :: !ByteString
- class ToServerEvent a where
- toServerEvent :: a -> ServerEvent
- data ServerSentEvents (a :: Type)
- data EventStream
- type RecommendedEventSourceHeaders (a :: Type) = Headers '[Header "X-Accel-Buffering" Text, Header "Cache-Control" Text] a
- recommendedEventSourceHeaders :: a -> RecommendedEventSourceHeaders a
Server-Sent Events
Event streams are implemented using servant's Stream endpoint.
You should provide a handler that returns a stream of events that implements
ToSourceIO where events have a ToServerEvent instance.
Example:
type MyApi = "books" :> ServerSentEvents (SourceIO Book)
instance ToServerEvent Book where
toServerEvent book = ...
server :: Server MyApi
server = streamBooks
where streamBooks :: Handler (SourceIO Book)
streamBooks = pure $ source [book1, ...]data ServerEvent Source #
Represents an event sent from the server to the client in Server-Sent Events (SSE).
Constructors
| ServerEvent | |
Fields
| |
Instances
class ToServerEvent a where Source #
This typeclass allows you to define custom event types that can be
transformed into the ServerEvent type, which is used to represent events in
the Server-Sent Events (SSE) protocol.
Methods
toServerEvent :: a -> ServerEvent Source #
Instances
| ToServerEvent ServerEvent Source # | |
Defined in Servant.API.EventStream Methods | |
data ServerSentEvents (a :: Type) Source #
A ServerSentEvents endpoint emits an event stream using the format described at https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
Instances
data EventStream Source #
A type representation of an event stream. It's responsible for setting proper content-type and buffering headers, as well as for providing parser implementations for the streams. Read more on Servant Streaming Docs
Instances
| Accept EventStream Source # | |
Defined in Servant.API.EventStream Methods contentType :: Proxy EventStream -> MediaType # | |
| ToServerEvent a => MimeRender EventStream a Source # | |
Defined in Servant.API.EventStream Methods mimeRender :: Proxy EventStream -> a -> ByteString # | |
Recommended headers for Server-Sent Events
This is mostly to guide reverse-proxies like nginx.
Example:
type MyApi = "books" :> ServerSentEvents (RecommendedEventSourceHeaders (SourceIO Book))
server :: Server MyApi
server = streamBooks
where streamBooks :: Handler (RecommendedEventSourceHeaders (SourceIO Book))
streamBooks = pure $ recommendedEventSourceHeaders $ source [book1, ...]type RecommendedEventSourceHeaders (a :: Type) = Headers '[Header "X-Accel-Buffering" Text, Header "Cache-Control" Text] a Source #
Recommended headers for Server-Sent Events.
recommendedEventSourceHeaders :: a -> RecommendedEventSourceHeaders a Source #
Add the recommended headers for Server-Sent Events to the response.