Skip to content

WebSocket Transport

The webSocket Transport connects to a JSON-RPC API via a WebSocket.

Import

import { webSocket } from "cive";

Usage

import { createPublicClient, webSocket } from "cive";
import { mainnet } from "cive/chains";
 
const client = createPublicClient({
  chain: mainnet,
  transport: webSocket("wss://main.confluxrpc.com/ws"), 
});

Parameters

url

  • Type: string

URL of the JSON-RPC API.

const transport = webSocket("wss://main.confluxrpc.org/ws");

keepAlive (optional)

  • Type: boolean | { interval?: number }
  • Default: true

Whether or not to send keep-alive ping messages.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  keepAlive: { interval: 1_000 }, 
});

key (optional)

  • Type: string
  • Default: "webSocket"

A key for the Transport.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  key: "conflux", 
});

name (optional)

  • Type: string
  • Default: "WebSocket JSON-RPC"

A name for the Transport

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  name: "conflux WebSocket Provider", 
});

reconnect (optional)

  • Type: boolean | { maxAttempts?: number, delay?: number }
  • Default: true

Whether or not to attempt to reconnect on socket failure.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  reconnect: false, 
});

reconnect.attempts (optional)

  • Type: number
  • Default: 5

The max number of times to attempt to reconnect.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  reconnect: {
    attempts: 10, 
  },
});

reconnect.delay (optional)

  • Type: number
  • Default: 2_000

Retry delay (in ms) between reconnect attempts.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  reconnect: {
    delay: 1_000, 
  },
});

retryCount (optional)

  • Type: number
  • Default: 3

The max number of times to retry when a request fails.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  retryCount: 5, 
});

retryDelay (optional)

  • Type: number
  • Default: 150

The base delay (in ms) between retries. By default, the Transport will use exponential backoff (~~(1 << count) * retryDelay), which means the time between retries is not constant.

const transport = webSocket("wss://main.confluxrpc.com/ws", {
  retryDelay: 100, 
});

timeout (optional)

  • Type: number
  • Default: 10_000

The timeout for async WebSocket requests.

const transport = webSocket('wss://main.confluxrpc.com/ws', {
  timeout: 60_000, 
})