QubicTypeScript

createNodeConnection

Open a direct TCP connection to a single Qubic node.

createNodeConnection opens a raw TCP socket to one node. For most production use, createNodePool is a better choice — it handles failover automatically. Use a direct connection when you're targeting a specific peer or building monitoring tooling.

createNodeConnection(host, options?)

import { createNodeConnection, requestCurrentTickInfo } from "@qubic.org/tcp"

const conn = await createNodeConnection("node1.qubic.org")
try {
  const { tick, epoch } = await requestCurrentTickInfo(conn)
  console.log(`Epoch ${epoch}, tick ${tick}`)
} finally {
  conn.close()
}

Parameters

NameTypeDefaultDescription
hoststringNode hostname or IP address
options.portnumber21841TCP port
options.timeoutMsnumber10000Connection and request timeout in milliseconds

Returns Promise<NodeConnection>

Throws QubicTcpConnectionError — if the connection cannot be established within the timeout.


conn.request(type, payload?, options?)

Sends a raw TCP frame and collects all response packets until END_RESPONSE. Handles TRY_AGAIN (server busy) automatically — waits 1 second and resends.

import { MESSAGE_TYPE } from "@qubic.org/tcp"

const packets = await conn.request(MESSAGE_TYPE.REQUEST_CURRENT_TICK_INFO)
// packets: Uint8Array[] — all response payloads
NameTypeDescription
typenumberMessage type constant from MESSAGE_TYPE
payloadUint8Array | undefinedOptional request payload
options.signalAbortSignal | undefinedCancels the request

Returns Promise<Uint8Array[]>

Throws QubicTcpTimeoutError — if the signal fires or the timeout is exceeded.


conn.close()

Closes the TCP socket. Always call this when done — open sockets block process exit.


Error types

ClassWhen thrown
QubicTcpErrorBase class
QubicTcpConnectionErrorCould not connect within timeout
QubicTcpTimeoutErrorRequest timed out or AbortSignal fired

On this page