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

Error: CONNECTION ERROR: Couldn't connect to node on WS. #140

Open
minhtruong92 opened this issue Sep 10, 2018 · 6 comments
Open

Error: CONNECTION ERROR: Couldn't connect to node on WS. #140

minhtruong92 opened this issue Sep 10, 2018 · 6 comments

Comments

@minhtruong92
Copy link

I use this code at https://infura.io/docs/wss/introduction

const Web3 = require("web3");

let web3 = new Web3(
  new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws")
);

const instance = new web3.eth.Contract(<abi>, <address>);

instance.getPastEvents(
    "allEvents",
    { fromBlock: 0, toBlock: "latest" },
    (errors, events) => {
        if (!errors) {
            // process events
        }
    }
);

I got error message when run:

Error: CONNECTION ERROR: Couldn't connect to node on WS.
at Object.InvalidConnection (/var/www/html/node_modules/web3-core-helpers/src/errors.js:35:16)
    at WebsocketProvider._timeout (/var/www/html/node_modules/web3-providers-ws/src/index.js:249:48)
    at W3CWebSocket.connection.onclose (/var/www/html/node_modules/web3-providers-ws/src/index.js:149:15)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/var/www/html/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onClose (/var/www/html/node_modules/websocket/lib/W3CWebSocket.js:228:10)
    at WebSocketConnection.<anonymous> (/var/www/html/node_modules/websocket/lib/W3CWebSocket.js:201:17)
    at emitTwo (events.js:126:13)
    at WebSocketConnection.emit (events.js:214:7)
    at WebSocketConnection.handleSocketClose (/var/www/html/node_modules/websocket/lib/WebSocketConnection.js:387:14)
    at emitOne (events.js:121:20)
    at TLSSocket.emit (events.js:211:7)
    at _handle.close (net.js:557:12)
    at TCP.done [as _onclose] (_tls_wrap.js:356:7)

I'm using web3 version 1.0.0-beta.36.

@sanaterrae
Copy link

sanaterrae commented Oct 13, 2018

Same issue, it will connect to the WS, then about 5 seconds later will close the connection.

Workflow: I have some code to reconnect the WS on close and it successfully says it reconnects. It then goes on to instantiate some event listeners and that is when this error shows up. It say's it reconnected, and the code has worked to this point and actually did reconnect. When attempting to start the event listeners the error "connection not open on send()" doesn't show up so I assume it is 'connected' but something else if failing.

Some other things to note in my situation: Same code works perfectly for Ropsten test-net replacing "wss://mainnet.infura.io/ws" with "wss://ropsten.infura.io/ws".

Also with web3 version 1.0.0-beta.20 the error states: Error: CONNECTION ERROR: Couldn't connect to node on IPC (instead of WS) <-- Not sure if that is relevant at all.

Lastly, if I re-deploy my application many times, once in awhile with the same code it'll work and properly connect/stay connected.

@mswezey23
Copy link

mswezey23 commented Nov 26, 2018

Confirming - ran into this issue today.

Saw something about TLS 1.2 handshake being required?
https://github.com/INFURA/documentation/issues/49

Anyone confirm that works?

@AdvaithD
Copy link

AdvaithD commented Dec 9, 2018

Ran into this issue today, does anyone know what exactly is causing this?

@sudosammy
Copy link

I have the same issue - appears to only be on mainnet as ropsten works fine. The error returned on mainnet is Connection dropped by remote peer. Can be seen with this:

const wsprovider = new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')
wsprovider.on('end', e => console.error('WS End', e));
WS End _Event {
  type: 'close',
  isTrusted: false,
  _yaeti: true,
  code: 1006,
  reason: 'Connection dropped by remote peer.',
  wasClean: false,
  target:
   W3CWebSocket {
     _listeners: {},
     addEventListener: [Function: _addEventListener],
     removeEventListener: [Function: _removeEventListener],
     dispatchEvent: [Function: _dispatchEvent],
     _url: 'wss://mainnet.infura.io/ws',
     _readyState: 3,
     _protocol: undefined,
     _extensions: [],
     _bufferedAmount: 0,
     _binaryType: 'arraybuffer',
     _connection:
      WebSocketConnection {
        _debug: [Object],
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        _pingListenerCount: 0,
        config: [Object],
        socket: [Object],
        protocol: undefined,
        extensions: [],
        remoteAddress: '35.169.193.102',
        closeReasonCode: 1006,
        closeDescription: 'Connection dropped by remote peer.',
        closeEventEmitted: true,
        maskOutgoingPackets: true,
        maskBytes: <Buffer a0 d7 e3 f1>,
        frameHeader: <Buffer 00 00 00 00 00 00 00 00 00 00>,
        bufferList: [Object],
        currentFrame: [Object],
        fragmentationSize: 0,
        frameQueue: [],
        connected: false,
        state: 'closed',
        waitingForCloseResponse: false,
        receivedEnd: true,
        closeTimeout: 5000,
        assembleFragments: true,
        maxReceivedMessageSize: 8388608,
        outputBufferFull: false,
        inputPaused: false,
        receivedDataHandler: [Function: bound ],
        _closeTimerHandler: [Function: bound ],
        webSocketVersion: 13,
        socketHadError: false },
     _client:
      WebSocketClient {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        config: [Object],
        _req: null,
        protocols: [],
        origin: undefined,
        url: [Object],
        secure: true,
        base64nonce: 'e/Rc5SynJA1RjYJL5dy5iA==',
        socket: [Object],
        response: [Object],
        firstDataChunk: null },
     onerror: [Function],
     onclose: [Function],
     onmessage: [Function] },
  cancelable: true,
  stopImmediatePropagation: [Function] }

For reference, the code that is executed before this error is:

myContract.events.allEvents({
  address: "0x3c62d30f6e6d3cfed....",
  filter: {internalID: '2'},
  fromBlock: 0,
  toBlock: 'latest',
}, function(error, event) {
  if (error) { console.log(error) }
  console.log(event);
})

Did anyone have luck figuring this out? I am also using web3js 1.0.0-beta.36

@ryanschneider
Copy link

@sudosammy this looks to be related to your logs query, which is scanning the entire blockchain and thus taking too long to complete so the websocket connection appears idle and you are disconnected.

Try using a smaller query on only the most recent, say, ~1000 blocks. The data from older blocks will never change so should be cached in your Dapp, not queried from the chain each time the Dapp is loaded.

The reason this works on ropsten is that its a much smaller chain with less events, so the query can be performed in time.

@sudosammy
Copy link

Thanks @ryanschneider - you're completely right, I was being too lazy with my filter. Cheers.

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

No branches or pull requests

6 participants