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://')

// Other stuff happens

module.exports = {
    // 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:

        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?

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

2 Answers 2


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

new Web3.providers.WebsocketProvider(your_WSS_Url_Here);

  • 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. Dec 30, 2022 at 19:03

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 that you have read and understand our privacy policy and code of conduct.

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