Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket onMessage error with string (parse) #3383

Open
1 task done
yogaajs opened this issue Feb 26, 2025 · 2 comments
Open
1 task done

Websocket onMessage error with string (parse) #3383

yogaajs opened this issue Feb 26, 2025 · 2 comments
Labels
Good First Issue Misc: Good First Issue

Comments

@yogaajs
Copy link

yogaajs commented Feb 26, 2025

Check existing issues

Viem Version

2.21.55

Current Behavior

Some provider return a string on error, here getBlock.io, return a string "rate limit reached" instead of an object:

Error

SyntaxError: Unexpected token 'r', "rate limit reached" is not valid JSON
    at JSON.parse (<anonymous>)
    at WebSocket.onMessage (.../node_modules/viem/utils/rpc/webSocket.ts:39:25)

Websocket.ts

      function onMessage({ data }: MessageEvent) {
        onResponse(JSON.parse(data))
      }

I'm calling RPC function "getBlockReceipts" after subscribing to newHeads event.

Expected Behavior

No response

Steps To Reproduce

  import { createPublicClient, webSocket, http, toHex } from 'viem'
  import { mainnet } from 'viem/chains'
  
  const client = createPublicClient({
      chain: mainnet,
      transport: webSocket(providers.getblock.base.wss),
  });

  const startBlock = blockNumber - 10n;
  const blockNumbers = Array.from({ length: 10 }, (_, i) => toHex(startBlock + BigInt(i)));
  
  // Create an array of promise calls for getBlockReceipts.
  const promises = blockNumbers.map((hexBlockNumber) =>
      client.request({
          method: "eth_getBlockReceipts",
          params: [hexBlockNumber],
      })
  );

  try {
      // Wait for all the receipt calls to resolve concurrently.
      const receipts = await Promise.all(promises);
      console.log('Block receipts:', receipts?.[0]?.blockNumber);
  } catch (error) {
      console.error('Error fetching block receipts:', error);
  }

Link to Minimal Reproducible Example

No response

Anything else?

No response

@jxom jxom added the Good First Issue Misc: Good First Issue label Feb 26, 2025
@Swarnim-Chandve
Copy link

Swarnim-Chandve commented Feb 27, 2025

Hello, I would like to work on this issue.

@yogaajs
Copy link
Author

yogaajs commented Feb 27, 2025

Check: #3389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Misc: Good First Issue
Projects
None yet
Development

No branches or pull requests

3 participants