bburdette/websocket/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

WebSocket

WebSocket.

This WebSocket Elm module lets you encode and decode messages to pass to javascript, where the actual websocket sending and receiving will take place. See the README for more.

= Connect
{ name : String
, address : String
, protocol : String
}
| Send
{ name : String
, content : String
}
| Close { name : String }

messages going out from Elm to be processed in javascript. name: You should give each websocket connection a unique name. address: is the websocket address, for instance "ws://127.0.0.1:9000". protocol: is an extra string to help the server know what kind of data to expect, like if your server handled json or binary data. Probably you can pass it "".

= Error
{ name : String
, error : String
}
| Data
{ name : String
, data : String
}

responses from javascript after websocket operations. The name is the one you have the socket in Connect.

decode incoming messages from the websocket javascript.

encode websocket commands into json.

receive : (Result Error WebSocketMsg -> msg) -> Value -> msg

make a subscription function with receive and a port, like so:

  port receiveSocketMsg : (JD.Value -> msg) -> Sub msg


  wsreceive =
      receiveSocketMsg <| WebSocket.receive WsMsg

Where WsMessage is defined in your app like this:

  type Msg
      = WsMsg (Result JD.Error WebSocket.WebSocketMsg)
      | <other message types>

then in your application subscriptions:

  subscriptions =
      \_ -> wsreceive
send : (Value -> Cmd msg) -> WebSocketCmd -> Cmd msg

use send to make a websocket convenience function, like so:

  port sendSocketCommand : JE.Value -> Cmd msg


  wssend =
      WebSocket.send sendSocketCommand

then you can call (makes a Cmd):

  wssend <|
      WebSocket.Send
          { name = "touchpage"
          , content = dta
          }