kageurufu / elm-websockets / Websockets

Simple interfaces for getting started with Websockets in Elm

Other modules are exposed for advanced usage.

Defining your Ports

withPorts : { command : CommandPort msg, event : EventPort msg } -> Methods msg

Helper to wrap your Ports


type alias CommandPort msg =
Json.Encode.Value -> Platform.Cmd.Cmd msg

Port to send Commands to the Websocket


type alias EventPort msg =
(Json.Encode.Value -> msg) -> Platform.Sub.Sub msg

Port for subscribing to income events


type alias Methods msg =
{ open : String -> String -> List ( String
, Maybe String ) -> Platform.Cmd.Cmd msg
, close : String -> Platform.Cmd.Cmd msg
, send : String -> Json.Encode.Value -> Platform.Cmd.Cmd msg
, onEvent : EventHandlers msg -> Platform.Sub.Sub msg 
}

Methods for using your sockets

Event data received from your Sockets


type alias EventHandlers msg =
{ onOpened : WebsocketOpened -> msg
, onClosed : WebsocketClosed -> msg
, onError : WebsocketError -> msg
, onMessage : WebsocketMessage -> msg
, onDecodeError : String -> msg 
}

Event handlers for subscriptions


type alias WebsocketOpened =
{ name : Event.Name
, meta : Meta 
}

Record data when a socket is Opened


type alias WebsocketMessage =
{ name : Event.Name
, meta : Meta
, data : String 
}

Record data when a socket is Message


type alias WebsocketClosed =
{ name : Event.Name
, meta : Meta
, reason : String 
}

Record data when a socket is Closed


type alias WebsocketError =
{ name : Event.Name
, meta : Meta
, error : Maybe String 
}

Record data when a socket is Error