Cannot receive data through websocket request

Hey there,
I’m struggling with receiving data through a websocket. What I did up till now was to open the channel so the connection is live but I don’t receive information about the requested item. Could somebody please point me to the right direction? I followed the instructions under Authentication and used the exact same object structure but to no avail:

const request = {
  type: 'subscribe',
  channels: ['ticker'],
  product_ids: ['BTC-USD'],
  signature: btoa(secret),
  key: api_key,
  passphrase: pass_phrase,
  timestamp: Date.now() / 1000,
};

this.messages = <Subject<any>>websocketService.connect(WS_ENDPOINT).pipe(
      map((response: MessageEvent): any => {
        let content = JSON.parse(response.data);
        return {
          user: content.user,
          messageContent: content.messageContent,
        };
      })
    );

this.messages.subscribe((msg: any) => {
      console.log(JSON.stringify(msg));
    });

    setTimeout(() => {
      this.messages.next(JSON.stringify(request));
    }, 2000);

I’m working with Angular btw.

Hello @marcopolocs! Thank you for taking an interest in Coinbase services. For the details regarding your concern, we will check on this for you with our team. We will get back to you once we have more information. Keep in touch!

2 Likes

Hello @marcopolocs. I’m not familiar with angular so I cannot help you troubleshoot your concern. But I suggest that you first try to subscribe to the WebSocket via a tool like Postman before integrating to the programming language/framework of your choice. Not sure if this will help but here are the steps I made when I subscribed to the WebSocket via Postman:

  1. Generate a signature, you can follow the steps provided in the documentation at https://docs.cloud.coinbase.com/exchange/docs/authorization-and-authentication.

Here is the script I used in generating a signature:

  1. Import crypto-js library var CryptoJS = require(“crypto-js”);
  2. Create the JSON request object var req = { timestamp: Math.floor(Date.now()/1000), // seconds since Unix epoch method: pm.request.method, path: pm.request.url.getPathWithQuery(), body: (pm.request.method === ‘GET’ || !data) ? ‘’ : pm.request.body, message: undefined, secret: CryptoJS.enc.Base64.parse(pm.collectionVariables.get(“coinbase-api-secret”)), // read value from collection variable hmac: undefined, signature: undefined, };
  3. Create the message to be signed req.message = req.timestamp + req.method + req.path + req.body;
  4. Create HMAC using message and API secret req.hmac = CryptoJS.HmacSHA256(req.message, req.secret);
  5. Obtain signature by converting HMAC to hexadecimal String req.signature = req.hmac.toString(CryptoJS.enc.Base64);
  6. Set Postman request’s authentication headers for Coinbase REST API call pm.collectionVariables.set(“coinbase-api-timestamp”, req.timestamp); pm.collectionVariables.set(“coinbase-api-signature”, req.signature);
  1. Added the necessary HTTP headers and sent a request to the endpoint https://api.exchange.coinbase.com/users/self/verify or https://api.pro.coinbase.com/users/self/verify (depending if you have a Coinbase Pro or Coinbase Exchange account) as stated in the websocket authorization documentation to generate a signature.

Headers:

CB-ACCESS-KEY:{{coinbase-api-key}}
CB-ACCESS-PASSPHRASE:{{coinbase-api-passphrase}}
CB-ACCESS-TIMESTAMP:{{coinbase-api-timestamp}}
CB-ACCESS-SIGN:{{coinbase-api-signature}}
CB-VERSION:2021-09-04
Content-Type:application/json

  1. Subscribed to the websocket channel using the generated signature.

Subscription message:

{ “type”: “subscribe”,
“product_ids”: [
“BTC-USD”
],
“channels”: [
“user”
],
“signature”: “…”,
“key”: “…”,
“passphrase”: “…”,
“timestamp”: “…”
}

I see that you are subscribing to the ticker channel, please note that the ticker channel does not require authentication. The ticker channel provides real-time price updates every time a match happens. It shows market data, a publicly available information. You only need to authenticate to a WebSocket channel that returns private information regarding your account such as the user channel.

I hope this helps. Thank you!

2 Likes