0

I'm using Web3.js and am trying to monitor the Ethereum mainnet. Up until now I have been testing on a local environment, but I want to test it live now. Here's the relevant code:

const Web3 = require('web3')
let web3

if (!config.PROJECT_SETTINGS.isLocal) {
    web3 = new Web3(`wss://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`) 
    // web3 = new Web3(`wss://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`)
} else {
    web3 = new Web3('ws://127.0.0.1:7545')
}

// Other stuff happens

module.exports = {
    web3,
    // It exports other stuff too, but this is what matters for the question.
}

As you can see, when a setting isLocal is set to false, it should begin pulling data from an API key. However, I continue to receive the error in the subject line. Here's the full error log:

/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:66
        const error = new Error(msg);
                      ^

Error: connection not open on send()
    at Object.ConnectionError (/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:66:23)
    at Object.ConnectionNotOpenError (/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:49:21)
    at /Users/Me/bot/node_modules/web3-providers-ws/lib/index.js:155:37
    at Map.forEach (<anonymous>)
    at WebsocketProvider._onClose (/Users/Me/bot/node_modules/web3-providers-ws/lib/index.js:154:27)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/Me/bot/node_modules/yaeti/lib/EventTarget.js:115:12)
    at W3CWebSocket.onConnectFailed (/Users/Me/bot/node_modules/websocket/lib/W3CWebSocket.js:219:14)
    at WebSocketClient.<anonymous> (/Users/Me/bot/node_modules/websocket/lib/W3CWebSocket.js:59:25)
    at WebSocketClient.emit (node:events:513:28)
    at ClientRequest.handleRequestError (/Users/Me/bot/node_modules/websocket/lib/WebSocketClient.js:227:14)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 1006,
  reason: 'connection failed'
}

I have seen from other similar posts that simply refreshing the API key is enough to make this work. However, I have tried multiple API keys from Infura and Alchemy. Each time, I have received the same error message.

Any other ideas about why this might be failing?

1
  • No idea if bumping works anymore, but I'm bumping in hopes of an answer. Commented Dec 31, 2022 at 6:59

2 Answers 2

0

Ok If You want to use WebSocket Connection in web3js try using

new Web3.providers.WebsocketProvider(your_WSS_Url_Here);

1
  • Thanks for the thought! Unfortunately didn't work. Same error. I even tried HttpProvider as well (using a https URL of course) and that failed as well. Commented Dec 30, 2022 at 19:03
0

Solved my own issue here.

Changed web3 = new Web3(`wss://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`)

To web3 = new Web3(Web3.givenProvider || `wss://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.