| Copyright | (c) 2024 Shaun Sharples |
|---|---|
| License | BSD3 |
| Stability | alpha |
| Safe Haskell | None |
| 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
- data EventStream
- type RecommendedEventSourceHeaders a = 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 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
| HasForeignType lang ftype a => HasForeign (lang :: k) ftype (ServerSentEvents a) Source # | a helper instance for servant-foreign | ||||
Defined in Servant.API.EventStream Associated Types
Methods foreignFor :: Proxy lang -> Proxy ftype -> Proxy (ServerSentEvents a) -> Req ftype -> Foreign ftype (ServerSentEvents a) # | |||||
| HasLink (ServerSentEvents a :: Type) Source # | |||||
Defined in Servant.API.EventStream Methods toLink :: (Link -> a0) -> Proxy (ServerSentEvents a) -> Link -> MkLink (ServerSentEvents a) a0 # | |||||
| (ToServerEvent chunk, ToSourceIO chunk a, GetHeaders (Headers h a)) => HasServer (ServerSentEvents (Headers h a) :: Type) context Source # | |||||
Defined in Servant.API.EventStream Methods route :: Proxy (ServerSentEvents (Headers h a)) -> Context context -> Delayed env (Server (ServerSentEvents (Headers h a))) -> Router env # hoistServerWithContext :: Proxy (ServerSentEvents (Headers h a)) -> Proxy context -> (forall x. m x -> n x) -> ServerT (ServerSentEvents (Headers h a)) m -> ServerT (ServerSentEvents (Headers h a)) n # | |||||
| (ToServerEvent chunk, ToSourceIO chunk a) => HasServer (ServerSentEvents a :: Type) context Source # | |||||
Defined in Servant.API.EventStream Methods route :: Proxy (ServerSentEvents a) -> Context context -> Delayed env (Server (ServerSentEvents a)) -> Router env # hoistServerWithContext :: Proxy (ServerSentEvents a) -> Proxy context -> (forall x. m x -> n x) -> ServerT (ServerSentEvents a) m -> ServerT (ServerSentEvents a) n # | |||||
| Generic (ServerSentEvents a) Source # | |||||
Defined in Servant.API.EventStream Associated Types
Methods from :: ServerSentEvents a -> Rep (ServerSentEvents a) x # to :: Rep (ServerSentEvents a) x -> ServerSentEvents a # | |||||
| type Foreign ftype (ServerSentEvents a) Source # | |||||
Defined in Servant.API.EventStream | |||||
| type MkLink (ServerSentEvents a :: Type) r Source # | |||||
Defined in Servant.API.EventStream | |||||
| type ServerT (ServerSentEvents (Headers h a) :: Type) m Source # | |||||
Defined in Servant.API.EventStream | |||||
| type ServerT (ServerSentEvents a :: Type) m Source # | |||||
Defined in Servant.API.EventStream | |||||
| type Rep (ServerSentEvents a) Source # | |||||
Defined in Servant.API.EventStream | |||||
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 = 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.