I have a Script that is reading events from the Ethereum blockchain and want to use infura.io instead of my full parity client. Unfortunately I get the following error as soon as I change the in this code to my Infura.io Access link.

 var fs = require("fs");
var Web3 = require('web3');

var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(''));

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/Users/moritzhubel/node_modules/web3/lib/web3/errors.js:38:16)
at XMLHttpRequest.request.onreadystatechange (/Users/moritzhubel/node_modules/web3/lib/web3/httpprovider.js:116:24)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/moritzhubel/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/Users/moritzhubel/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpRequestError (/Users/moritzhubel/node_modules/xhr2/lib/xhr2.js:544:12)
at ClientRequest.<anonymous> (/Users/moritzhubel/node_modules/xhr2/lib/xhr2.js:414:24)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)

Is there anyone who can help?

  • Infura doesn't support events. I think a solution is to use something like provider engine which has a filter sub provider. – Ismael Jan 15 '18 at 15:14

Using the latest web3.js version (the one that is installed with a simple npm i web3) the following should work:

const web3 = new Web3('https://mainnet.infura.io/')

  • Unfortunately I get another error node_modules/web3/lib/web3/requestmanager.js:80 this.provider.sendAsync(payload, function (err, result) { ^ TypeError: this.provider.sendAsync is not a function at RequestManager.sendAsync – Hu Mor Jan 14 '18 at 16:46
  • Can you do const web3 = new Web3();console.log(web3.version) and see which version it gives you? – smirzo Jan 14 '18 at 17:05
  • Just in case...have you tried connecting to the Infura link without your access token (so just 'https://mainnet.infura.io/'). Maybe the problem lies within the link itself. – smirzo Jan 14 '18 at 17:17
  • I get version 0.20.3 as a response; yes I tried it is not the link... – Hu Mor Jan 14 '18 at 17:51
  • That is definitely stranger. I tried installing version 0.20.3 and running the same commands as you did and everything seemed to be running ok. Does it fail straight after running the setProvider command or does it fail only when you call a function on it? The only way I was really able to replicate this error was changing the RPC url. – smirzo Jan 14 '18 at 18:41

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