billstclair / elm-websocket-framework-server / WebSocketServer

Web socket server enables you to write the server part of your websocket application in Elm.

This is copied from the upgraded version of RGBBoy/websocket-server at github.com/billstclair/websocket-server. You probably won't need to use it, except via WebSocketFramework.Server.

Web Socket Server


type alias Socket =
String

A pointer to the socket in the node.js world. These are based on uuids and are unique to each connection that is created.


type alias Location =
{ href : String
, host : String
, hostname : String
, protocol : String
, origin : String
, port_ : String
, pathname : String
, search : String
, hash : String
, username : String
, password : String 
}

The same Location type as found in elm-lang/navigation in Elm 0.17.

eventDecoder : { onConnection : Socket -> Location -> msg, onDisconnection : Socket -> Location -> msg, onMessage : Socket -> Location -> String -> msg } -> Json.Decode.Decoder msg

Create a decoder to use with your input port. This allows you to hook into the events that will be triggered over a sockets lifetime and respond to them in your update function.

onConnection : Socket -> Location -> msg

Triggered when a new connection is made. Can be used to get the new connection and the Location that the connection was made to. This can be useful to segregate connections into groups or associating a private id.

onDisconnection : Socket -> Location -> msg

Triggered when a disconnection happens. Can be used to clean up the connection from anywhere it has been saved in your application state.

onMessage : Socket -> Location -> String -> msg

Triggered when a socket recieves a message.

Note 1: Almost everyone will want to use a URL parsing library like evancz/url-parser to turn a Location into something more useful.

Commands

sendToOne : (Json.Encode.Value -> a) -> String -> Socket -> a

Send a message to a particular socket. Given you have an output port:

port outputPort : Encode.Value -> Cmd msg

You would write something like this to create a cmd to send a message:

sendToOne outputPort "Hello!" socketA

sendToMany : (Json.Encode.Value -> a) -> String -> List Socket -> List a

Send a message to a many sockets. Given you have an output port:

port outputPort : Encode.Value -> Cmd msg

You would write something like this to create a cmd to send messages:

sendToMany outputPort "Hello!" [ socketA, socketB ]
    |> Cmd.batch

sendToOthers : (Json.Encode.Value -> a) -> String -> Socket -> List Socket -> List a

Send a message to a all sockets except one. Given you have an output port:

port outputPort : Encode.Value -> Cmd msg

You would write something like this to create a cmd to send messages:

sendToOthers outputPort "Hello!" socketA [ socketA, socketB, socketC ]
    |> Cmd.batch

close : (Json.Encode.Value -> a) -> Socket -> a

Close a socket connection. Given you have an output port:

port outputPort : Encode.Value -> Cmd msg

You would write something like this to create a cmd to close a socket:

close outputPort socketA