MetaMask / web3-provider-engine

A JavaScript library for composing Ethereum provider objects using middleware modules
MIT License
597 stars 328 forks source link

Error during WebSocket handshake connecting to Infura #292

Closed slyfox42 closed 5 years ago

slyfox42 commented 5 years ago

Hi everyone.

Whenever I try to connect to Infura using the websocket subprovider, this error pops up:

WebSocket connection to 'wss://ropsten.infura.io/ws/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

It does work when connecting to Ganache.

I am using the latest version, 14.1.0 My code:

const init = async (web3: Web3 = window.web3) => {
  const engine = new ProviderEngine()
  engine.addProvider(new SignerSubprovider(window['web3'].currentProvider))
  engine.addProvider(new FilterSubprovider())
  const subscriptionSubprovider = new SubscriptionSubProvider()
  engine.addProvider(subscriptionSubprovider)
  subscriptionSubprovider.on('data', (err, notification) => {
    return engine.emit('data', err, notification)
  })
  engine.addProvider(
    new WebSocketSubProvider({
      rpcUrl: 'wss://ropsten.infura.io/ws/'
    })
  )

  const enginePromise = new Promise((resolve, reject) => {
    engine.start(err => (err ? reject(err) : resolve()))
  })

  try {
    await enginePromise
  } catch (err) {
    console.error(err)
  }

  return engine
}

The problem seems to lie at this particular line on the websocket subprovider: https://github.com/MetaMask/provider-engine/blob/master/subproviders/websocket.js#L145

If I change this._socket = new WebSocket(this._url, null, {origin: this._origin}) to this._socket = new WebSocket(this._url, [], {origin: this._origin}) then the connection will work correctly.

I noticed there are 2 open PRs that involve this same line of code and that have gotten no response, #286 and #290, but neither of them addresses this issue, as leaving that second parameter null or undefined will cause the error.

I am happy to open the PR to fix this if you guys are ok with that, let me know.

milonite commented 5 years ago

@slyfox42 I think you can go for a PR, i get the same error and changing that line fix it.