lue-bird / elm-state-interface / Web.Socket

Helpers for Socket types as part of an Interface

connection

connectTo : String -> Web.Interface Web.SocketConnectionEvent

An Interface for opening a connection on a given address, notifying you when it's connected or disconnected

Once this detects it's available, make sure to set your state's SocketId so you can actually send and receive messages. And once it's disconnected, set your state's SocketId back to nothing:

case state.socketId of
    Nothing ->
        Web.Socket.connectTo "ws://127.0.0.1:9000"
            |> Web.interfaceMap
                (\connectionChanged ->
                    case connectionChanged of
                        Web.SocketConnected socketId ->
                            { state | socketId = socketId |> Just }

                        Web.SocketDisconnected ->
                            { state | socketId = Nothing }
                )

    Just socketId ->
        Web.Socket.message socketId "Meow"

disconnect : Web.SocketId -> Web.Interface future_

An Interface for closing a given connection

communicate

message : Web.SocketId -> String -> Web.Interface future_

An Interface for sending data to the server.

It's common to pair this with Json.Encode.encode 0 to send json.

messageListen : Web.SocketId -> Web.Interface String

An Interface for detecting when data has been sent from the server