> ## Documentation Index
> Fetch the complete documentation index at: https://cosmos-docs-cosmjs-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# @cosmjs/socket

> Cross-platform WebSocket client utilities

Low-level WebSocket client for browser and Node.js environments. Used internally by `@cosmjs/tendermint-rpc` for WebSocket connections and streaming subscriptions.

```bash theme={"system"}
npm install @cosmjs/socket
```

## SocketWrapper

Thin abstraction over the native WebSocket API that works in both browsers and Node.js.

| Method        | Parameters                                                                                                                                                                                                                                       | Returns         |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------- |
| `constructor` | `url: string`, `messageHandler: (event: SocketWrapperMessageEvent) => void`, `errorHandler: (event: SocketWrapperErrorEvent) => void`, `openHandler?: () => void`, `closeHandler?: (event: SocketWrapperCloseEvent) => void`, `timeout?: number` | `SocketWrapper` |
| `connect`     | —                                                                                                                                                                                                                                                | `void`          |
| `disconnect`  | —                                                                                                                                                                                                                                                | `void`          |
| `send`        | `data: string`                                                                                                                                                                                                                                   | `Promise<void>` |

| Property    | Type                                               |
| ----------- | -------------------------------------------------- |
| `connected` | `Promise<void>` (resolves once the socket is open) |

## StreamingSocket

WebSocket client that exposes received messages as an xstream `Stream`.

| Method        | Parameters                        | Returns           |
| ------------- | --------------------------------- | ----------------- |
| `constructor` | `url: string`, `timeout?: number` | `StreamingSocket` |
| `connect`     | —                                 | `void`            |
| `disconnect`  | —                                 | `void`            |
| `send`        | `data: string`                    | `Promise<void>`   |

| Property    | Type                                |
| ----------- | ----------------------------------- |
| `events`    | `Stream<SocketWrapperMessageEvent>` |
| `connected` | `Promise<void>`                     |

## ReconnectingSocket

WebSocket client with automatic reconnection logic.

| Method         | Parameters                                                           | Returns              |
| -------------- | -------------------------------------------------------------------- | -------------------- |
| `constructor`  | `url: string`, `timeout?: number`, `reconnectedHandler?: () => void` | `ReconnectingSocket` |
| `connect`      | —                                                                    | `void`               |
| `disconnect`   | —                                                                    | `void`               |
| `queueRequest` | `request: string`                                                    | `void`               |

| Property           | Type                                |
| ------------------ | ----------------------------------- |
| `events`           | `Stream<SocketWrapperMessageEvent>` |
| `connectionStatus` | `ValueAndUpdates<ConnectionStatus>` |

## QueueingStreamingSocket

WebSocket that queues outgoing messages until the connection is established.

| Method           | Parameters                                                           | Returns                   |
| ---------------- | -------------------------------------------------------------------- | ------------------------- |
| `constructor`    | `url: string`, `timeout?: number`, `reconnectedHandler?: () => void` | `QueueingStreamingSocket` |
| `connect`        | —                                                                    | `void`                    |
| `disconnect`     | —                                                                    | `void`                    |
| `queueRequest`   | `request: string`                                                    | `void`                    |
| `getQueueLength` | —                                                                    | `number`                  |

| Property           | Type                                |
| ------------------ | ----------------------------------- |
| `events`           | `Stream<SocketWrapperMessageEvent>` |
| `connectionStatus` | `ValueAndUpdates<ConnectionStatus>` |

## Event Types

```typescript theme={"system"}
interface SocketWrapperMessageEvent {
  readonly data: string;
  readonly type: string;
}

interface SocketWrapperErrorEvent {
  readonly isTrusted?: boolean;
  readonly type?: string;
  readonly message?: string;
}

interface SocketWrapperCloseEvent {
  readonly wasClean: boolean;
  readonly code: number;
}
```

## ConnectionStatus

```typescript theme={"system"}
enum ConnectionStatus {
  Unconnected,  // 0
  Connecting,   // 1
  Connected,    // 2
  Disconnected, // 3
}
```
