Coinbase Institutional CBExchange JavaScript SDK example: publicWs.ts

Coinbase Institutional CBExchange public websocket JavaScript example. Review the source code and SDK calls, then browse related integration examples.

This example uses coinbase-api, the JavaScript and TypeScript SDK for Coinbase. Install it with npm install coinbase-api.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Coinbase/Institutional/CBExchange/WebSockets/publicWs.ts

What this example covers

  • Coinbase WebSocket stream JavaScript example.
  • Uses the Siebly Coinbase SDK package coinbase-api instead of hand-written WebSocket plumbing.
  • Source path: Coinbase/Institutional/CBExchange/WebSockets/publicWs.ts.
  • Example category: Institutional CBExchange.
  • Imports SDK symbols including WebsocketClient.
  • Calls SDK methods such as on(), subscribe().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { WebsocketClient } from 'coinbase-api'; async function start() {  /**   * All websockets are available via the unified WebsocketClient.   *   * Below are examples specific to websockets in this product group.   */  const client = new WebsocketClient();   client.on('open', (data) => {    console.log('open: ', data?.wsKey);  });   // Data received  client.on('update', (data) => {    console.info(new Date(), 'data received: ', JSON.stringify(data));  });   // Something happened, attempting to reconnect  client.on('reconnect', (data) => {    console.log('reconnect: ', data);  });   // Reconnect successful  client.on('reconnected', (data) => {    console.log('reconnected: ', data);  });   // Connection closed. If unexpected, expect reconnect -> reconnected.  client.on('close', (data) => {    console.error('close: ', data);  });   // Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"  client.on('response', (data) => {    console.info('response: ', JSON.stringify(data, null, 2));    // throw new Error('res?');  });   client.on('exception', (data) => {    console.error('exception: ', data);  });   try {    /**     * Use the client subscribe(topic, market) pattern to subscribe to any websocket topic.     *     * - You can subscribe to topics one at a time or many in one request.     * - Topics can be sent as simple strings, if no parameters are required.     * - Coinbase Market Data is the traditional feed which is available without authentication.     * - To use this feed, use 'exchangeMarketData' as the wsKey for each subscription command:     */    // client.subscribe('status', 'exchangeMarketData');     /**     * Or send a more structured object with parameters, e.g. if parameters are required     */    // const tickerSubscribeRequest: WsTopicRequest = {    //   topic: 'ticker',    //   /**    //    * Anything in the payload will be merged into the subscribe "request",    //    * allowing you to send misc parameters supported by the exchange (such as `product_ids: string[]`)    //    */    //   payload: {    //     product_ids: ['ETH-USD', 'ETH-EUR'],    //   },    // };    // client.subscribe(tickerSubscribeRequest, 'exchangeMarketData');     /**     * Subscribe to the "heartbeat" topic for a few symbols     */    // client.subscribe(    //   {    //     topic: 'heartbeat',    //     payload: {    //       product_ids: ['ETH-USD', 'BTC-USD'],    //     },    //   },    //   'exchangeMarketData',    // );     // client.subscribe(    //   {    //     topic: 'level2',    //     payload: {    //       product_ids: ['ETH-USD', 'BTC-USD'],    //     },    //   },    //   'exchangeMarketData',    // );     // /**    //  * Or, send an array of structured objects with parameters, if you wanted to send multiple in one request    //  */    // // client.subscribe([level2SubscribeRequest, anotherRequest, etc], 'advTradeMarketData');     /**     * Other Coinbase Exchange public websocket topics:     */    client.subscribe(      [        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#heartbeat-channel        {          topic: 'heartbeat',          payload: {            product_ids: ['ETH-EUR'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#status-channel        'status',        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#auction-channel        {          topic: 'auctionfeed',          payload: {            product_ids: ['LTC-USD'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#matches-channel        {          topic: 'matches',          payload: {            product_ids: ['BTC-USD'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#rfq-matches-channel        {          topic: 'rfq_matches',          payload: {            // Optional:            // product_ids: ['ETH-USD', 'BTC-USD'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#ticker-channel        {          topic: 'ticker',          payload: {            product_ids: ['ETH-USD', 'BTC-USD'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#ticker-batch-channel        {          topic: 'ticker_batch',          payload: {            product_ids: ['ETH-USD', 'BTC-USD'],          },        },        // https://docs.cdp.coinbase.com/exchange/docs/websocket-channels#level2-batch-channel        {          topic: 'level2_batch',          payload: {            product_ids: ['ETH-USD', 'BTC-USD'],          },        },      ],      'exchangeMarketData',    );  } catch (e) {    console.error('Subscribe exception: ', e);  }} start(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.