RGBboy/websocket-server/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

WebSocketServer

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

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 =
Location

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

{ onConnection : Socket -> Location -> msg
, onDisconnection : Socket -> Location -> msg
, onMessage : Socket -> Location -> String -> msg
}
-> 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 : (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 : (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 : (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 : (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