Web sockets make it cheaper to talk to your servers.
Connecting to a server takes some time, so with web sockets, you make that connection once and then keep using. The major benefits of this are:
It faster to send messages. No need to do a bunch of work for every single message.
The server can push messages to you. With normal HTTP you would have to keep asking for changes, but a web socket, the server can talk to you whenever it wants. This means there is less unnecessary network traffic.
Opaque message type.
You can create the instances you need to send with openMessage, sendMessage, closeMessage, and bytesQueuedMessage.
A response that your code must process to update your model.
NoResponse means there's nothing to do.
CmdResponse is a Cmd that you must return from your update function. It will send something out the sendPort in your Config.
ConnectedReponse tells you that an earlier call to send or keepAlive has successfully connected. You can usually ignore this.
MessageReceivedResponse is a message from one of the connected sockets.
ClosedResponse tells you that an earlier call to close has completed. Its code, reason, and wasClean fields are as passed by the JavaScript WebSocket interface. Its expected field will be True, if the response is to a close call on your part. It will be False if the close was unexpected, and reconnection attempts failed for 20 seconds (using exponential backoff between attempts).
ErrorResponse means that something went wrong. Details in the encapsulated Error.
All the errors that can be returned in a Response.ErrorResponse.
If an error tag has a single String arg, that string is a socket key.
This will usually be NormalClosure. The rest are standard, except for UnknownClosure, which denotes a code that is not defined, and TimeoutOutOnReconnect, which means that exponential backoff connection reestablishment attempts timed out.
The standard codes are from https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
PortFunnel.FunnelSpecThe name of this module: "WebSocket".
Our module descriptor.
Responsible for sending a CmdResponse back through the port.
Called by PortFunnel.appProcess for each response returned by process.
StateThe initial, empty state.
MessageCreate a Message to open a connection to a WebSocket server.
makeOpen urlExample:
makeOpen "wss://echo.websocket.org"
|> send cmdPortCreate a Message to send a string to a particular address.
makeSend key messageExample:
makeSend "wss://echo.websocket.org" "Hello!"
|> send cmdPortYou must send a makeOpen or makeOpenWithKey message before makeSend.
If you send a makeSend message before the connection has been established, or while it is being reestablished after it was lost, your message will be buffered and sent after the connection has been (re)established.
Create a Message to close a previously opened WebSocket.
makeClose keyThe key arg is either they key arg to makeOpenWithKey or
makeKeepAliveWithKey or the url arg to makeOpen or makeKeepAlive.
Example:
makeClose "echo"
|> send cmdPortLike makeOpen, but allows matching a unique key to the connection.
makeOpen uses the url as the key.
makeOpenWithKey key urlExample:
makeOpenWithKey "echo" "wss://echo.websocket.org"Create a Message to connect to a WebSocket server, but not report received messages.
makeKeepAlive urlFor keeping a connection open for when you only need to send makeSend messages.
Example:
makeKeepAlive "wss://echo.websocket.org"
|> send cmdPortLike makeKeepAlive, but allows matching a unique key to the connection.
makeKeepAliveWithKey key urlExample:
makeKeepAliveWithKey "echo" "wss://echo.websocket.org"
|> send cmdPortMessage out the Cmd PortSend a Message through a Cmd port.
Convert a Message to a nice-looking human-readable string.
Convert a Message to the same JSON string that gets sent
over the wire to the JS code.
Convert an Error to a string, for simple reporting.
Turn a ClosedCode into a String, for debugging.
Make a simulated Cmd port.
Returns true if a Startup message has been processed.
This is sent by the port code after it has initialized.
Returns true if a connection is open for the given key.
isConnected key stateReturn True if the connection for the given key will be automatically reopened if it closes unexpectedly.
This is the default. You may change it with setAutoReopen.
willAutoReopen key stateSet whether the connection for the given key will be automatically reopened if it closes unexpectedly.
This defaults to True. If you would rather get a ClosedResponse when it happens and handle it yourself, set it to False before sending a makeOpen message.
You may change it back to False later. Changing it to True later will not interrupt any ongoing reconnection process.
setAutoReopen key autoReopenThe key is either the key you plan to use for a makeOpenWithKey or makeKeepAliveWithKey message or the url for a makeOpen or makeKeepAlive message.
Encode a Message into a GenericMessage.
Only exposed so the tests can use it.
User code will use it implicitly through moduleDesc.
Decode a GenericMessage into a Message.
Only exposed so the tests can use it.
User code will use it implicitly through moduleDesc.
Internal state of the WebSocketClient module.
Get the initial, empty state with
initialState.