bburdette/websocket/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

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 }

WebSocketCmds go from from elm out to javascript to be processed.

  • 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 either json or binary data. Probably you can just pass it "".
  • content: the data you're sending through the socket.
= Error
{ name : String
, error : String
}
| Data
{ name : String
, data : String
}

WebSocketMsgs are responses from javascript to elm after websocket operations. The name should be the same string you used 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
          }