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 decoderTo 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.
Apply the connection transition, then decode any accepted text event. Socket
filtering, retry actions, and connection state follow Connection.update.
Decode a text event while preserving establishment, binary data, and transport
errors. Use this to adapt individual events after calling Connection.update.
Typed application events.
PayloadDecodeFailureis separate from transport errors; neither event automatically triggers reconnection.