# Module /websocket/client

WebSocket client supporting text and binary frames, ping/pong, and graceful close.

```tangu
(use /websocket/client)
```

## Functions & symbols

### close

`(close ws) -> None`

Sends a close frame and shuts down the WebSocket connection.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ws` | `WebSocket-Client` | no | WebSocket client handle |

```tangu
(ws/close ws)
```

### connect

`(connect url [headers [options]]) -> WebSocket-Client`

Connects to a WebSocket server and returns an open `WebSocket-Client` handle.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `url` | `String` | no | WebSocket endpoint (`ws://` or `wss://`) |
| `headers` | `Map` | yes | Optional handshake headers |
| `options` | `Map` | yes | Optional connection config |

```tangu
(let ws (ws/connect "wss://echo.websocket.events"))
```

### is-open?

`(is-open? ws) -> Bool`

Returns `true` while the WebSocket connection remains open.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ws` | `WebSocket-Client` | no | WebSocket client handle |

```tangu
(ws/is-open? ws)
```

### receive

`(receive ws [non-blocking]) -> (Or Map None)`

Receives the next WebSocket frame `{:type :text|:binary|:ping|:pong|:close :data ...}`, or `()`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ws` | `WebSocket-Client` | no | Open WebSocket client |
| `non_blocking` | `Bool` | yes | Optional non-blocking poll flag |

```tangu
(let msg (ws/receive ws))
```

### send

`(send ws message) -> None`

Sends a text (`String`) or binary (`Slab`) WebSocket frame.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ws` | `WebSocket-Client` | no | Open WebSocket client |
| `message` | `String | Slab` | no | Text or binary payload to transmit |

```tangu
(ws/send ws "hello")
```
