Connection Issues Websocket API - Testnet Websocket API Not working

I am unable to connect the websocket api on testnet. Is there any current known issue with this?

Can someone try to connect using these function see if it works ,please?

export async function getAllOrdersFromBinance(symbol: string): Promise<Order[]> {
  return new Promise( async (resolve, reject) => {
    const listenKey = await getUserDataStream();
    const wsUserData = new WebSocket(`${wsTestURL2}/${listenKey}`);

    if (!testApiSecret) {
      throw new Error('No test API secret provided');
    }
    const requestId = uuidv4();
    wsUserData.on('open', () => {
      const timeStamp = Date.now();
      const queryString = `symbol=${symbol}&timestamp=${timeStamp}`;
      const signature = crypto.createHmac("sha256", testApiSecret).update(queryString).digest("hex");

      const params = {
        symbol: symbol.toUpperCase(),
        apikey: testApiKey,
        signature: signature,
        timestamp: timeStamp,
      }
      const message = {
        id: requestId,
        method: 'allOrders',
        params: params,
        limit: 500,
      } 
      wsUserData.send(JSON.stringify(message));
    });

    wsUserData.on('message', (message: string) => {
      const data = JSON.parse(message) as BinanceResponse;
      console.log('Received order status from binance:', data);
      if (data.id === requestId) {
        resolve(data.result);
      }
    });

    wsUserData.on('error', (error) => {
      reject(error);
    });
  });
} 
export async function getOrderStatusFromBinance(symbol: string, orderId: number): Promise<Order> {
  return new Promise(async (resolve, reject) => {
    const listenKey = await getUserDataStream();
    const wsUserData = new WebSocket(`${wsTestURL}/${listenKey}`)
    if (!testApiSecret) {
      throw new Error('No test API secret provided');
    }
    const requestId = uuidv4();
    wsUserData.on('open', () => {
      const timeStamp = Date.now();
      const queryString = `symbol=${symbol}&orderId=${orderId}&timestamp=${timeStamp}`;
      const signature = crypto.createHmac("sha256", testApiSecret).update(queryString).digest("hex");
      console.log(queryString)
      const params = {
        symbol: symbol.toUpperCase(),
        orderId: orderId,
        apiKey: testApiKey,
        signature: signature,
        timestamp: timeStamp,
      }
      const message = {
        id: requestId,
        method: 'order.status',
        params: params,
      }
      console.log("message", message)
      wsUserData.send(JSON.stringify(message));
    });

    wsUserData.on('message', (message: string) => {
      const data = JSON.parse(message) as { id: string; result: Order };
       console.log('order status', data);
      if (data.id === requestId) {
        resolve(data.result);
      }
    });

    wsUserData.on('error', (error) => {
      reject(error);
    });

     // Timeout to reject the promise if no response is received within 10 seconds
     setTimeout(() => {
      wsUserData.close(); // Close the WebSocket connection
      reject(new Error('Request timed out'));
    }, 10000);
  });
}

fyi wstesturl2 and wsurl have same value, one is exported from .env file another is save on top of the function. const wsTestURL2 = “wss://testnet.binance.vision/ws-api/v3”
i am having errror message 404 and close code 1006, why?

duplicate of How to use Websocket API for order.status endpoint? - #8 by Leonardll . Solution is address in this discussion.

1 Like