miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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

EventSource

connectText Source #

Arguments

:: URL

URL endpoint for the Server-Sent Events stream

-> (EventSource -> action)

onOpen callback; receives the live EventSource handle

-> (MisoString -> action)

onMessage callback; receives each raw text message

-> (MisoString -> action)

onError callback; receives an error description

-> 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 live EventSource handle (store it in the model to close it later)
  • onMessage — called with each message payload as a plain string
  • onError — called with a description of the connection error

See https://developer.mozilla.org/en-US/docs/Web/API/EventSource

connectJSON Source #

Arguments

:: FromJSON value 
=> URL

URL endpoint for the Server-Sent Events stream

-> (EventSource -> action)

onOpen callback; receives the live EventSource handle

-> (value -> action)

onMessage callback; receives each JSON-decoded message

-> (MisoString -> action)

onError callback; receives an error description

-> 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

close Source #

Arguments

:: EventSource

The EventSource handle to close

-> 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 #

Retrieves current status of WebSocket

If the WebSocket identifier does not exist a CLOSED is returned.

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 

type URL = MisoString Source #

URL that the WebSocket will connect to

Re-exports

data Payload value Source #

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