Batch request with websocket transport #4375
-
|
Hi, The multicall works but it seems that I can't override the sender address , I have contract execution errors (I am on a private blockchain and don't have access to the aggregate3 smart contract) I wanted to make sure that batch calling is currently not implemented with the websocket transport or if I am doing it wrong my current config batch call test : The rpc batch requests works with the websocket transport as rpc request accept either one rpc json {id,data...} or a list of rpc json [{id,data...}]. a simple example I noted that overriding the it does not open a second websocket connection either which is good, I am just worried about a potential memory leak playing with the onmessage Is there a more 'viem native' way to do it, it would also be nice if the batching also works automagically like with the http transport so different eth calls could be merged in one batch for efficiency reason |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I checked the current docs and source, and there are really 2 different batching paths in viem here.
So for the transport part, there is no native WebSocket version of There is also one detail in your example that explains why your reads are not being merged. So what you are seeing is expected for this shape of call: viemContract.read.getElementMetaData([e], { account: address })If you want the viem native route for many reads with a custom sender, I would use const results = await publicClient.multicall({
account: address,
contracts: idList.map((id) => ({
address: contractAddress,
abi: contractAbi,
functionName: 'getElementMetaData',
args: [id],
})),
deployless: true,
allowFailure: false,
})If your chain has a deployed Multicall3, use I would also avoid overriding So short answer:
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the clarification, as we manage our nodes we can enable the http server in parallel of the websocket server on our nodes As we are transitionning form web3js to viem, I will temporarly use an implementation tweak to enable batch request on websocket, with a more optimized version of the script I provided in my post in oder to minimize the friction during the migration. For now I don't see any side effect but i understand that overriding the on message is tricky Is there performance drawbacks for using websocket over http for batch and single request ? Our workflow is the user loads of list of elements at login (can be thousands) but then only use the events to update the list (+perform basic read/write on smart contract) |
Beta Was this translation helpful? Give feedback.
I checked the current docs and source, and there are really 2 different batching paths in viem here.
http(..., { batch: true })is transport level JSON-RPC batching.That exists on the HTTP transport.
createPublicClient({ batch: { multicall: ... } })iseth_callaggregation through Multicall3 or deployless multicall.That is client level batching, not WebSocket JSON-RPC batching.
So for the transport part, there is no native WebSocket version of
http({ batch: true })today.The current
webSocket()transport sends one request at a time.There is also one detail in your example that explains why your reads are not being merged.
When you pass
account, viem turns that intofromon thee…