bennycode / coinbase-pro-node

Coinbase API written in TypeScript and covered by tests.
https://bennycode.com/coinbase-pro-node
MIT License
254 stars 62 forks source link

Maybe update docs for passing channels? #726

Closed kenchambers closed 2 years ago

kenchambers commented 2 years ago

reproduction

const { CoinbasePro } = require('coinbase-pro-node');

class CoinbaseProAPI {
  constructor({ apiKey, apiSecret, apiPassphrase, test }) {
    this.apiKey = apiKey;
    this.apiSecret = apiSecret;
    this.apiPassphrase = apiPassphrase;
    this.test = test;
    this.client = this.initializeClient();
  }

  pureWebsocketTest = () => {
    // 2. Setup WebSocket channel info
    const channel = {
      name: 'WebSocketChannelName.TICKER',
      product_ids: ['BTC-USD', 'ETH-EUR'],
    };

    // 3. Wait for open WebSocket to send messages
    this.client.ws.on('WebSocketEvent.ON_OPEN', async () => {
      // 7. Subscribe to WebSocket channel
      await this.client.ws.subscribe([channel]);
    });

    // 4. Listen to WebSocket subscription updates
    this.client.ws.on('WebSocketEvent.ON_SUBSCRIPTION_UPDATE', subscriptions => {
      console.log(subscriptions);
      // When there are no more subscriptions...
      if (subscriptions.channels.length === 0) {
        // 10. Disconnect WebSocket (and end program)
        // this.client.ws.disconnect();
      }
    });

    // 5. Listen to WebSocket channel updates
    this.client.ws.on('WebSocketEvent.ON_MESSAGE_TICKER', async tickerMessage => {
      console.log('ON MSG TICKER');
      // console.log(ticketMessage);
      // 8. Receive message from WebSocket channel
      // console.info(`Received message of type "${tickerMessage.type}".`, tickerMessage);
      // 9. Unsubscribe from WebSocket channel
      // await this.client.ws.unsubscribe([
      //   {
      //     name: 'WebSocketChannelName.TICKER',
      //     product_ids: ['BTC-USD'],
      //   },
      // ]);
    });

    // 6. Connect to WebSocket
    this.client.ws.connect({ debug: true });
  };

  initializeClient = () => {
    const auth = {
      apiKey: this.apiKey,
      apiSecret: this.apiSecret,
      passphrase: this.apiPassphrase,
      useSandbox: this.test,
    };

    const client = new CoinbasePro(auth);
    return client;
  };
}

After using your example here: https://github.com/bennycode/coinbase-pro-node/blob/main/src/demo/websocket-ticker.ts

When i console log subscriptions inside of WebSocketEvent.ON_SUBSCRIPTION_UPDATE

I am not getting any channels?

output:

RWS> connect { url: 'wss://ws-feed.pro.coinbase.com', protocols: [] }
RWS> addListeners
RWS> open event
RWS> send {"channels":[{"name":"WebSocketChannelName.TICKER","product_ids":["BTC-USD","ETH-EUR"]}],"type":"subscribe","key":"288dc1e9ffcddaddae56989c1d7f4c92","passphrase":"0547sjouiel","signature":"FFYW5HlQKoZyO2ALqnxVebo0uFaXiagJurD4kcdOHOs=","timestamp":1652228605.752}
RWS> message event
RWS> message event
RWS> message event
{ type: 'subscriptions', channels: [] } // console.log
RWS> accept open

Maybe docs just need to be updated?

thanks so much! great library

kenchambers commented 2 years ago

Figured it out! i had the wrong string for channel:

wrong:

  const channel = {
      name: 'WebSocketChannelName.TICKER',
      product_ids: ['BTC-USD', 'ETH-EUR'],
    };

right:

  const channel = {
      name: "ticker",
      product_ids: ['BTC-USD', 'ETH-EUR'],
    };