Web3JS Socket/Connection Issues
Dealing with Socket/Connection issues in Web3JS
Socket connection issues are encountered while using the `@solana/web3.js` library to communicate with RPCs
Errors
The errors you’ve faced might be like the following
Cause
These errors are caused by NodeJS. It uses too many local ports to communicate with the server. At some point, this hits a limit and existing connections are broken and new ones are not allowed causing the errors. You can check the number of ports used by your Node process via the following command on UNIX based systems.
These errors originate from the `undici` library; Not by web3.js or your RPC. This library is NodeJS’ implementation of the `fetch` function used to make API calls. Following are some references to this issue w.r.t. `undici`
Links |
---|
We’ve been in touch with Anza Labs and the maintainers of `@solana/web3.js` and have confirmed that this issue is not with web3.js.
Fixes
A fix to these issues is to limit the number of ports NodeJS can use. This can be done easily from your JS code. We've set the number of allowed ports to 100 in our tests and performed over 2 million asynchronous getMultipleAccounts
calls with 1000 addresses in each call. All of these calls resolved in ~2mins. You are free to set the number as you see fit.
What is an ideal connection count for you?
Each established connection needs a TLS handshake. More details here. Establishing connection is time a consuming process and in this case is unnecessary since existing connections can be reused. It'll also be comparatively less load on the RPC as well. We recommend setting the connection limit to 50. You can monitor the number of connections using the command mentioned above in the Causesection. And, if you feel like all connections are always in use and your application can benefit from more connections then you can increase the limit.
Configuring
undici
Using Axios for API calls instead of `undici`. The `@solana/web3.js` library allows you to specify a custom fetch function in the connection object. You can use Axios there. Here’s an example
Here's a full example on using axios with web3.js https://gist.github.com/WilfredAlmeida/9adea27abb5958178c4370c5656e89b7
Using the Bun runtime. Bun is a new JS runtime. As per our tests, these issues aren’t encountered at all via Bun. Please note that bun is quite new and may cause other problems.
Using the new version of `@solana/web3.js`. Anza Labs is developing a new and improved version of the library which, as of this writing, is in technical preview. It uses the `undici` library and the errors are less frequent in it. If encountered, with the configuration mentioned in [1] they should be resolved. Do note that the library is not compatible with the current version, meaning that it’ll be a ground up rewrite of your codebase.
Other SDK/Libraries
If you're using SDKs like jito-ts which uses node-fetch underneath, or some other libraries, you may encounter errors like ERR_STREAM_PREMATURE_CLOSE
which have this same underlying issue and limiting the number of connections for NodeJS has resolved the issue for our customers. The number of connections for libraries like node-fetch
and others can be limited by providing an http(s).Agent
configuration. For example
We’re continuously working on this issue. Improvements and progress will be added to this document.
We've proposed that the @solana/web3.js
library have a default configurable connection limit. You can follow an upvote the discussion on GitHub.
Last updated