| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.EventSource
Description
Interface to the browser's Server-Sent Events API, which provides a persistent, unidirectional stream of text or JSON messages pushed from a server to the browser client.
Typical usage inside an update function:
update FetchStream =
connectText "/events" StreamOpened GotMessage StreamError
update (GotMessage msg) =
modify (\m -> m { messages = msg : messages m })
close shuts down an open EventSource connection. socketState reads
the current ready-state, and emptyEventSource provides a zero-value for
use in the model before a connection is established.
Synopsis
- connectText :: URL -> (EventSource -> action) -> (MisoString -> action) -> (MisoString -> action) -> Effect parent props model action
- connectJSON :: FromJSON value => URL -> (EventSource -> action) -> (value -> action) -> (MisoString -> action) -> Effect parent props model action
- close :: EventSource -> Effect parent props model action
- socketState :: WebSocket -> (SocketState -> action) -> Effect parent props model action
- emptyEventSource :: EventSource
- newtype EventSource = EventSource Int
- type URL = MisoString
- data Payload value
- = JSON value
- | BLOB Blob
- | TEXT MisoString
- | BUFFER ArrayBuffer
EventSource
Arguments
| :: URL | URL endpoint for the Server-Sent Events stream |
| -> (EventSource -> action) |
|
| -> (MisoString -> action) |
|
| -> (MisoString -> action) |
|
| -> Effect parent props model action |
Open a Server-Sent Events connection that delivers raw MisoString messages.
The three callbacks map browser events to action values dispatched into
the MVU loop:
onOpen— receives the liveEventSourcehandle (store it in the model tocloseit later)onMessage— called with each message payload as a plain stringonError— called with a description of the connection error
See https://developer.mozilla.org/en-US/docs/Web/API/EventSource
Arguments
| :: FromJSON value | |
| => URL | URL endpoint for the Server-Sent Events stream |
| -> (EventSource -> action) |
|
| -> (value -> action) |
|
| -> (MisoString -> action) |
|
| -> Effect parent props model action |
Open a Server-Sent Events connection that decodes each message as JSON.
Identical to connectText but the onMessage callback receives a parsed
Haskell value of type value (via FromJSON) rather than a raw string.
JSON decode failures are silently dropped; use connectText and decode
manually if you need error recovery.
See https://developer.mozilla.org/en-US/docs/Web/API/EventSource
Arguments
| :: EventSource | The |
| -> Effect parent props model action |
Close an open EventSource connection.
After calling close, no further onMessage or onError callbacks will
fire. Corresponds to
EventSource.close().
socketState :: WebSocket -> (SocketState -> action) -> Effect parent props model action Source #
Defaults
emptyEventSource :: EventSource Source #
A null EventSource is one with a negative descriptor.
Types
newtype EventSource Source #
A type for holding an EventSource descriptor.
Constructors
| EventSource Int |
Instances
| Num EventSource Source # | |
Defined in Miso.Runtime Methods (+) :: EventSource -> EventSource -> EventSource # (-) :: EventSource -> EventSource -> EventSource # (*) :: EventSource -> EventSource -> EventSource # negate :: EventSource -> EventSource # abs :: EventSource -> EventSource # signum :: EventSource -> EventSource # fromInteger :: Integer -> EventSource # | |
| Eq EventSource Source # | |
Defined in Miso.Runtime | |
| ToJSVal EventSource Source # | |
Defined in Miso.Runtime | |
type URL = MisoString Source #
URL that the WebSocket will connect to
Re-exports
Payload is used as the potential source of data when working with EventSource
Constructors
| JSON value | JSON-encoded data |
| BLOB Blob | Binary encoded data |
| TEXT MisoString | Text encoded data |
| BUFFER ArrayBuffer | Buffered data |