A WebSocket client instance for the Krist API. Must be created with createWsClient.

After creating a KristWsClient, you can call connect to establish a new connection to the Krist server. After this, the client will automatically attempt to reconnect if the connection is lost.

KristWsClient is a standard event emitter. Events can be listened to with ws.on(event, callback).

When disconnecting from the Krist server, the client will automatically try to reconnect, starting with a 1 second delay, doubling with each attempt up to a maximum of 60 seconds (1 minute). The library will not stop attempting to reconnect - this is the responsibility of the caller.

Hierarchy

  • TypedEmitter<KristWsClientEvents>
    • KristWsClient

Properties

connectDebounceMs: number = DEFAULT_CONNECT_DEBOUNCE_MS
connectionState: WsConnectionState = "connecting"
forceClosing: boolean = false
initSubscriptions: KristWsSubscription[] = []
maxReconnectSecs: number = 60
messageId: number = 1
messageResponses: Record<number, { reject: ((reason?: KristWsS2CResponseMessage) => void); resolve: ((value?: KristWsS2CResponseMessage) => void) }> = {}
privatekey?: string
reconnectionTimer?: any
wsp?: WebSocketAsPromised
BLOCK: string = "block"

Emitted when a block is submitted, if subscribed to the blocks or ownBlocks events. The whole KristWsBlockEvent message will be passed to the callback.

Event

EVENT: string = "event"

Emitted when any event is received. The full KristWsEventMessage will be passed to the callback.

Event

KEEPALIVE: string = "keepalive"

Emitted when the server sends a keepalive ping. The server's time as a Date will be passed to the callback.

Event

MESSAGE: string = "message"

Emitted when any message is received. The full raw KristWsS2CMessage will be passed to the callback.

Event

NAME: string = "name"

Emitted when a name is purchased, if subscribed to the names or ownNames events. The whole KristWsNameEvent message will be passed to the callback.

Event

READY: string = "ready"

Emitted when the client has connected to the server and is ready to receive messages. The hello message from the server will be passed as an argument to the event, containing full KristMotd information. This event will happen each time the client connects to the server, so it may happen more than once.

Event

STATE: string = "state"

Emitted when the connection state changes. The new and old states respectively will be passed as arguments to the callback.

Event

TRANSACTION: string = "transaction"

Emitted when a transaction is made, if subscribed to the transactions or ownTransactions events. The whole KristWsTransactionEvent message will be passed to the callback.

Event

defaultMaxListeners: number

API Fetch

getAddress: ((this: KristWsClient, address: string, fetchNames?: boolean) => Promise<KristAddress>) = getAddress

Type declaration

getMe: ((this: KristWsClient) => Promise<KristWsS2CMe>) = getMe

Type declaration

getSubscriptions: ((this: KristWsClient) => Promise<KristWsS2CGetSubscriptionLevel>) = getSubscriptions

Type declaration

getWork: ((this: KristWsClient) => Promise<KristWsS2CWork>) = getWork

Type declaration

API Submit

makeTransaction: ((this: KristWsClient, to: string, amount: number, options?: KristWsMakeTransactionOptions) => Promise<KristTransaction>) = makeTransaction

Type declaration

API Miscellaneous

subscribe: ((this: KristWsClient, event: KristWsSubscription) => Promise<KristWsS2CSubscribe>) = subscribe

Type declaration

unsubscribe: ((this: KristWsClient, event: KristWsSubscription) => Promise<KristWsS2CUnsubscribe>) = unsubscribe

Type declaration

Methods

  • Returns Promise<void>

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Initiate a connection to the Krist server. From this point onwards, the client will take over responsibility for auto-reconnecting. Event handlers should be registered before calling this method.

    The promise will be resolved when the connection has been opened (but will not necessarily be ready yet).

    Returns Promise<void>

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • Rest ...args: Parameters<KristWsClientEvents[U]>

    Returns boolean

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Returns U[]

  • Forcibly close the websocket connection.

    Returns void

  • Returns number

  • Parameters

    • event: { code: number; reason: string }
      • code: number
      • reason: string

    Returns void

  • Parameters

    • Optional _err: Error

    Returns void

  • Parameters

    • type: keyof KristWsClientEvents

    Returns number

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • type: U

    Returns KristWsClientEvents[U][]

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • type: U

    Returns KristWsClientEvents[U][]

  • Parameters

    • Optional event: keyof KristWsClientEvents

    Returns KristWsClient

  • Type Parameters

    • U extends keyof KristWsClientEvents

    Parameters

    • event: U
    • listener: KristWsClientEvents[U]

    Returns KristWsClient

Advanced

  • Sends a raw message to the Krist server and waits for a response. The methods for a specific API request should be used instead of this function.

    Returns

    The response from the server.

    Parameters

    • data: C2S_T

      The full message to send to the server. The message ID will be generated automatically.

    Returns Promise<S2C_T>

Generated using TypeDoc