RGBboy / websocket-server / WebSocketServer

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

Web Socket Server


type alias Socket =
Internal.Socket

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

socketToString : Socket -> String

Transform a Socket into a String. Useful if you want to use the value as a key in a Dict.

eventDecoder : { onConnection : Socket -> Url -> msg, onDisconnection : Socket -> Url -> msg, onMessage : Socket -> Url -> 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 -> Url -> msg

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

onDisconnection : Socket -> Url -> 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 -> Url -> String -> msg

Triggered when a socket recieves a message.

Note: Almost everyone will want to use a URL parsing library like elm/url to turn a Url 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 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