I am using web3 version 1.0 version 26, however I can't receive any events from ganache-cli and testrpc.

  this.Contract.events.Created({},{ fromBlock: 0, toBlock: 'latest' }, function(error, event){ console.log(event); })
                        .on('data', function(event){

this is my provider connection

this.web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

is httpprovider not working for the watch events? do I have to use ws provider?

  • The HttpProvider should work fine (and does, in my experience). Can you share the rest of the code (e g. where you define this.Contract and where you generate the events)? – smarx Jan 1 '18 at 11:00
  • i have another question posted here, ethereum.stackexchange.com/questions/31747/…, you can check this. – user824624 Jan 1 '18 at 11:02
  • Sorry, I haven't used version 1.0.0 yet, and I was wrong. You do indeed need to use the WebsocketProvider. See my answer. – smarx Jan 1 '18 at 11:23

You're correct that the HttpProvider doesn't support subscribing to events in web3.js 1.0.0. From https://web3js.readthedocs.io/en/1.0/web3.html#value:

Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.

You will indeed need to use the WebsocketProvider (or IpcProvider for local nodes).

  • can ganache-cli or testrpc support ws right now or any other ways to start a testnet supporting ws – user824624 Jan 1 '18 at 11:25
  • No, as far as I can tell, ganache doesn't support websocket connections yet. See, e.g., github.com/trufflesuite/ganache-cli/issues/369. – smarx Jan 1 '18 at 11:29
  • But yes, geth supports it, so you should be able to spin up a test network that way. (I've never done this myself.) Or you could use an existing test network like Ropsten or Rinkeby. – smarx Jan 1 '18 at 11:30

I can confirm websocket connections are supported by Ganache. I used

  web3 = new Web3()
  const eventProvider = new Web3.providers.WebsocketProvider('ws://localhost:7545')

Subscriptions are currently also supported through the websocket API of Infura:

const RINKEBY_WSS = "wss://rinkeby.infura.io/ws";
var provider = new Web3.providers.WebsocketProvider(RINKEBY_WSS);
var web3 = new Web3(provider);
var name = 'newBlockHeaders'; // e.g.

web3.eth.subscribe(name, (error, result) => {
        if(!error) {
        } else {
            console.log('Error:', error);          
    }).on("data", function (transaction) {

See here for the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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