Websocket Effects Manager for Elm that works with BOTH front-end (browser) and back-end (node) programs. It allows for more sophisticated higher-level protocols than the default Websocket provided by Elm. It provides a message when the connection is lost allowing clients to employ their own reconnection strategy.
For example, when connecting to stateful back-end services, the client may need to re-authenticate or re-subscribe to that service.
You'll need Grove.
grove install panosoft/elm-websocket-client
This Effects Manager uses native code that relies on node-based code. Therefore, when this Effects Manager is used the browser, some package manager is required. Webpack is used in the Test application.
The buildBrowser.sh (and aBuildBrowser.sh) file(s) contains the Webpack command to build the test Browser program.
The output will be in a build directory. This file will be included by the Test/Browser/index.html file.
The buildNode.sh (and aBuildNode.sh) file(s) contains the build command to build the test Node program.
Connect to a Websocket Server
This must be done before any other commands are run.
Connections are maintained by the Effect Manager State and are referenced via urls.
connect : ConnectErrorTagger msg -> ConnectTagger msg -> Url -> Bool -> Cmd msg
connect errorTagger tagger url rejectUnauthorizedUsage
connect ConnectError Connect "wss://echo.websocket.org" TrueConnectErrorandConnectare your application's messages to handle the different scenarios.wss://echo.websocket.orgis the URL to the websocket serverrejectUnauthorizedshould be True unless testing with self-signed certificates (do not set to False in production!!!) This parameter is ignored if you're running in the Browser.
Send a message to the Websocket Server
Send a message to a specified URL. (A connection must already exist)
send : SendErrorTagger msg -> SendTagger msg -> Url -> String -> Cmd msg
send errorTagger tagger url messageUsage
send SendError Sent "wss://echo.websocket.org" "a string message"SendErrorandSentare your application's messages to handle the different scenarioswss://echo.websocket.orgis the URL to the websocket servera string messageis the message to send
Disconnect from a Websocket Server
When a connection is no longer needed, it can be disconnected.
disconnect : DisconnectErrorTagger msg -> DisconnectTagger msg -> Url -> Cmd msg
disconnect errorTagger tagger urlUsage
disconnect ErrorDisconnect SuccessDisconnect "wss://echo.websocket.org"ErrorDisconnectandSuccessDisconnectare your application's messages to handle the different scenarioswss://echo.websocket.orgis the URL to the websocket server
Listen for messages and events from a Websocket Server
Listen for messages and events from a specified URL (A connection must already exist)
listen : ListenErrorTagger msg -> MessageTagger msg -> ConnectionClosedTagger msg -> Url -> Sub msg
listen errorTagger messageTagger connectionClosedTagger url =Usage
listen ListenError Message ConnectionLost "wss://echo.websocket.org"ListenErroris your application's message to handle an error in listeningMessageis your application's message to handle received messagesConnectionLostis your application's message to handle when the server closes it's connectionwss://echo.websocket.orgis the URL to the websocket server
Error when connecting.
type alias ConnectErrorTagger msg =
( Url, ( ConnectErrorCode, ErrorMessage ) ) -> msgConnectErrorCode values are defined in the Websocket Protocol.
Usage
ConnectError ( url, (errorCode, errorMessage) ) ->
let
l =
Debug.log "ConnectError" ( url, (errorCode, errorMessage) )
in
model ! []Successful connection.
type alias ConnectTagger msg =
Url -> msgUsage
Connect url ->
let
l =
Debug.log "Connect" url
in
{ model | connected = True } ! []Error attempting to send.
type alias SendErrorTagger msg =
( Url, Message, ErrorMessage ) -> msgUsage
SendError ( url, message, error ) ->
let
l =
Debug.log "SendError" ( url, message, error )
in
model ! []Successful send.
type alias SendTagger msg =
( Url, Message ) -> msgUsage
Sent ( url, message ) ->
let
l =
Debug.log "Sent" ( url, message )
in
model ! []Error when disconnecting.
type alias DisconnectErrorTagger msg =
( Url, ErrorMessage ) -> msgUsage
DisconnectError ( url, error ) ->
let
l =
Debug.log "DisconnectError" ( url, error )
in
model ! []Successful disconnect.
type alias DisconnectTagger msg =
Url -> msgUsage
Disconnect url ->
let
l =
Debug.log "Disconnect" url
in
model ! []Error when attempting to listen.
type alias ListenErrorTagger msg =
( Url, ErrorMessage ) -> msgUsage
ListenError ( url, error ) ->
let
l =
Debug.log "ListenError" ( url, error )
in
{ model | listenError = True } ! []Message received from server.
type alias MessageTagger msg =
( Url, Message ) -> msgUsage
Message ( url, message ) ->
let
l =
Debug.log "Message" ( url, message )
in
model ! []Server closed the connection.
type alias ConnectionClosedTagger msg =
(Url, ConnectErrorCode, ErrorMessage) -> msgUsage
ConnectionLost (url, errorCode, errorMessage) ->
let
l =
Debug.log "ConnectionLost" (url, errorCode, errorMessage)
in
{ model | connected = False } ! []