Skip to content

WebSocket

Two WebSocket transports: one for chain events, one for bridge swap progress.

Use WebSocket in a browser showing live progress. For a server that must not miss an event, use webhooks — they are retried and survive your process restarting.

Chain events

wss://api.testnet.kriptonyx.com/socket/websocket?vsn=2.0.0

Phoenix channel protocol: join a topic, then receive events on it. Messages are JSON arrays framed as [join_ref, ref, topic, event, payload].

Topics

TopicEventPayload
blocks:new_blocknew_blockNewly indexed block
transactions:new_transactionnew_transactionNewly indexed transaction
addresses:{address_hash}balance, transactionPer-address updates
tokens:{address_hash}token_transferToken transfers

Joining a topic

js
const ws = new WebSocket('wss://api.testnet.kriptonyx.com/socket/websocket?vsn=2.0.0');

ws.onopen = () => {
  ws.send(JSON.stringify(['1', '1', 'blocks:new_block', 'phx_join', {}]));

  // Phoenix closes an idle socket; a heartbeat keeps it open.
  setInterval(() => {
    ws.send(JSON.stringify([null, '0', 'phoenix', 'heartbeat', {}]));
  }, 30000);
};

ws.onmessage = ({ data }) => {
  const [, , topic, event, payload] = JSON.parse(data);
  if (event === 'new_block') console.log('block', payload.block_number);
};

Send the heartbeat

Without a periodic phoenix heartbeat the server closes the connection as idle, and a client that does not reconnect silently stops receiving events.

Watching one address

js
ws.send(JSON.stringify(['1', '2', `addresses:${address.toLowerCase()}`, 'phx_join', {}]));

Address topics use the lowercase form.

Bridge swap events

wss://bridge.testnet.kriptonyx.com/ws

A simpler protocol: subscribe to one swap, receive its lifecycle.

js
const ws = new WebSocket('wss://bridge.testnet.kriptonyx.com/ws');

ws.onopen = () =>
  ws.send(JSON.stringify({ type: 'subscribe', swap_id: swapId }));

ws.onmessage = ({ data }) => {
  const msg = JSON.parse(data);

  if (msg.type === 'subscribed') console.log('watching', msg.swap_id);

  if (msg.type === 'swap_update') {
    console.log(msg.event, msg.tx_hash ?? '');
    if (msg.event === 'target_token_sent') console.log('done:', msg.tx_hash);
  }
};

The event names are the same catalogue used by webhooks, so the two transports are interchangeable.

Reconnecting

Both sockets can drop. Reconnect with backoff and re-join your topics, then reconcile anything missed:

  • Chain events — query the chain data API for the gap.
  • Bridge swaps — call GET /api/swap/:id/events, which returns the full lifecycle, so nothing is lost.

KriptoNyx testnet — chain ID 3009 (kriptonyx_3009-1)