A Guide to AJAX

XHR

WebSockets

Use webSocket API from the Reflex.Dom.WebSocket module.:

webSocket :: Text -> WebSocketConfig t a -> m (WebSocket t)

data WebSocketConfig t a
  = WebSocketConfig {_webSocketConfig_send :: Event t [a],
                     _webSocketConfig_close :: Event t (Word, Text),
                     _webSocketConfig_reconnect :: Bool}

 type WebSocket t =
   RawWebSocket t ByteString

 data RawWebSocket t a
   = RawWebSocket {_webSocket_recv :: Event t a,
                   _webSocket_open :: Event t (),
                   _webSocket_error :: Event t (),
                   _webSocket_close :: Event t (Bool, Text)}

To send data over WebSocket pass an event to _webSocketConfig_send of type Event t [a] where a is either Text or ByteString.

The return value from WebSocket is available from _webSocket_recv :: Event t ByteString

Here _webSocketConfig_close is an Event which can close the WebSocket connection from client side. And _webSocket_close is the response from server when the connection closes.

Manually closing a websocket that is configured to reconnect will cause it to reconnect. If you want to be able to close it permanently you need to set _webSocketConfig_reconnect = False.

See reflex-examples for an echo example.