Skip to content
Velo

channels.custom

Builds a channel with a decoder of your own. The decoder's return type determines the data received by the channel's listener.

Imports

import { Velo } from 'velo-sdk';

Example

This decoder reads the close price from a product's raw price frame:

const velo = new Velo({ apiKey: process.env.VELO_API_KEY! });
 
const closePrice = velo.channels.custom({
  kind: 'app.price-close',
  name: 'realtime_binance-futures:BTCUSDT',
  decode: (frame) => {
    const values = frame.d;
    if (
      !Array.isArray(values) || values.length !== 6 ||
      !values.every(
        (value) => value === null || (typeof value === 'number' && Number.isFinite(value)),
      ) ||
      typeof frame.tt !== 'number' || !Number.isFinite(frame.tt)
    ) {
      throw new Error('Invalid price frame');
    }
    return { closePrice: values[3] as number | null, tickTime: frame.tt };
  },
}).on({
  data: (data) => console.log(data.closePrice, data.tickTime),
  error: ({ reason }) => console.error('Custom channel error:', reason),
});
 
const watcher = await velo.watch(velo.channels.feed([closePrice]), {
  on: { error: (error) => console.error('Connection error:', error) },
});
 
// Later:
watcher.close();

Definition

function custom<const Kind extends string, Data>(
  definition: CustomChannel<Kind, Data>,
): Channel<Kind, Data>;

Parameters

definition

  • Type: CustomChannel<Kind, Data>
  • Required
PropertyTypeDescription
kindstringA non-empty name identifying how the payload is decoded. Use a name distinct from the built-in kinds.
namestringThe exact wire name. It must be non-empty with no surrounding whitespace or control characters.
decode(frame: ChannelFrame) => DataValidates and decodes the frame synchronously. Throw when the frame cannot be decoded.

The decoder receives the complete frame, including c, optional d and tt, and any channel-specific fields. These payload fields are untrusted data that your decoder must validate. Payload values can be null when the server sends no value; accept them, or those frames are reported as decode errors. Async decoders and promise-like return values are not supported.

Use a distinct kind for each decoder.

Return Type

  • Type: Channel<Kind, Data>

Returns a channel typed with the decoder's return value.