mbr/elm-wss/3.0.0

WebsocketSimple.Connection.Json

JSON messages over a managed connection. Keep using Connection.Model, Connection.subscribe, and Connection.perform; replace Connection.update with update, supplying your message decoder.

import Json.Decode as Decode
import Json.Encode as Encode
import WebsocketSimple.Connection as Connection
import WebsocketSimple.Connection.Json as Json


type alias Message =
    { text : String }


decoder : Decode.Decoder Message
decoder =
    Decode.map Message (Decode.field "text" Decode.string)


encoder : Message -> Encode.Value
encoder message =
    Encode.object [ ( "text", Encode.string message.text ) ]


receive : Connection.Msg -> Connection.Model -> ( Connection.Model, Connection.Action, Maybe (Json.Event Message) )
receive =
    Json.update decoder

To reply, use Json.send Ports.wsCmd encoder message connection.

A PayloadDecodeFailure retains the original text and decoding error without changing the connection. Your application can report it, ignore it, or call Connection.fail to reconnect according to its retry policy. Binary frames pass through unchanged, unlike the JSON adapter in WebsocketSimple.

type Event value
= Established (Maybe String)
| Received value
| PayloadDecodeFailure String Error
| Binary Bytes
| Error TransportErrorDetails

Typed application events. PayloadDecodeFailure is separate from transport errors; neither event automatically triggers reconnection.

Decoder value
-> Msg
-> Model
-> ( Model, Action, Maybe (Event value) )

Apply the connection transition, then decode any accepted text event. Socket filtering, retry actions, and connection state follow Connection.update.

parseIncoming : Decoder value -> Event -> Event value

Decode a text event while preserving establishment, binary data, and transport errors. Use this to adapt individual events after calling Connection.update.

send : CommandPort msg -> (value -> Value) -> value -> Model -> Cmd msg

Encode a value and send it as a text frame. Uses the same best-effort behavior as Connection.send: disconnected sends are discarded without buffering or replay.