Web3.js: web3.eth.Contract.allEvents does not work in Web3 1.0.0-beta16 using WebSockets

Created on 16 Aug 2017  路  20Comments  路  Source: ChainSafe/web3.js

Using allEvents in Web3 1.0.0 does not return any events. My code is as follows:

this.contract.events.allEvents({
  fromBlock: 0,
  toBlock: 'latest'
}, (err, event) => {
  console.log(err, event)
})

Using getPastEvents works. It seems like the subscription is never given an ID(?):

Subscription {
  id: null,
  callback: [Function],
  arguments:
   [ { toBlock: 'latest',
       address: '0x59c3b17749502874f237467818dfe46170ba92d8',
       topics: [] } ],
  _reconnectIntervalId: null,
  options:
   { subscription:
      { params: 1,
        inputFormatter: [Array],
        outputFormatter: [Function: bound ],
        subscriptionHandler: [Function: subscriptionHandler] },
     type: 'eth',
     requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     params:
      { toBlock: 'latest',
        address: '0x59c3b17749502874f237467818dfe46170ba92d8',
        topics: [] } },
  subscriptionMethod: 'logs' }

I am using geth 1.6.7-stable.

Most helpful comment

@alexey-ernest
started geth node with:

geth --rpcapi "db,eth,net,web3,personal" --ws --wsaddr "localhost" --wsport "8545" --wsorigins "*" --identity "MyTestNode" --datadir "./data" --testnet --fast

connected to web3:

const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));

listening:

contract.events.allEvents({ fromBlock: 'latest' }, console.log)

All 20 comments

I am using web3 1.0.0 and I have somewhat the same problem.
I did some test with this simple contract (using testrpc as the rpc provider) :

contract eventc {

event Top(string yolo);

function event_testing(string test){
  Top(test);
}
}
contract_event.methods.event_testing("tessssst").send({from: address_pers,gas: 1500000,
      gasPrice: 15000000}).then(console.log);

contract_event.events.Top({
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, event){
console.log("event : ",event);
});

The console only return : event : null (with the method transaction) despite multiple events in the blockchain.
As for me the getPastEvents works fine.

Hello,

Same for me, getPastEvents works allEvents does not.

The following code raises:

const eventHandler = clientReceipt.events.allEvents((error, event) => {
    if(error) {
        throw error
    }

    console.log('Event:');
    console.log(event);
});
Error: Returned error: Method not found
    at Object.ErrorResponse (/home/vagrant/app/node_modules/web3/packages/web3-core-helpers/src/errors.js:29:16)
    at Object.<anonymous> (/home/vagrant/app/node_modules/web3/packages/web3-core-requestmanager/src/index.js:137:36)
    at callback (/home/vagrant/app/node_modules/web3/packages/web3-providers-ipc/src/index.js:65:40)
    at Array.forEach (<anonymous>)
    at Socket.<anonymous> (/home/vagrant/app/node_modules/web3/packages/web3-providers-ipc/src/index.js:76:51)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)
    at readableAddChunk (_stream_readable.js:253:11)
    at Socket.Readable.push (_stream_readable.js:211:10)
    at Pipe.onread (net.js:585:20)

Versions:

  • Web3 1.0.0-beta.18
  • Parity 1.7.0

Also having this issue, getPastEvents() works like a dream but allEvents() and consequently contract.events.Event() do not work as expected.

They do not poll but will return an (unreliable) result under very specific circumstances as outlined below...

  • If _optional_ fromBlock argument is specified as the current block
  • Subscription is instantiated at the perfect moment in time with the _current block number_. If you continue to stop and restart (if node) or refresh (if browser) with the same _current_ fromBlock, different results are returned for each, and sometimes nothing is returned. The results are definitely not reliable. Once there's a new block, refreshing will return no results (assuming you have set fromBlock statically). _It will not however watch new events no matter how much you beg, yell or throw milkshakes at your computer_

It should be noted that passing {} as an argument doesn't seem to return any result unlike the above edge case, despite being noted in the documentation that the arguments are optional.

_It's possible the aforementioned edge case may shed light on the source of this problem, sans the milkshake_

I've tried both WS and IPC transport with similar results.

  • web3 1.0.0-beta.18
  • Parity 1.5 through 1.7

same story with: web3.eth.subscribe ;(

I subscribed to the event but it was not working. The callback was never called. Then I found out that I needed to activate the WebSockets server (--ws and similar in geth).
Found it in this Ethereum Stack Exchange Post

Issuing the same problem with 1.0.0-beta.24
When trying to in nodejs script

contract.events.allEvents({ fromBlock: 'latest' })
    .on('data', console.log)
    .on('changed', console.log)
    .on('error', console.log)

Nothing happens, script doesn't log anything and just exits while expecting it to hang and listen to the events, when I change code to this:
contract.events.allEvents({ fromBlock: 'latest' }, console.log)
Getting the error
Error: The current provider doesn't support subscriptions: HttpProvider

Connecting to local json rpc client
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
which launched with following params
geth --rpcapi "db,eth,net,web3,personal,web3" --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --identity "MyTestNode" --datadir "./data" --testnet --fast

Tested with websocket providers - everything works fine

@Oxyaction could you please share the full code snippet, can't make it work either.

@alexey-ernest
started geth node with:

geth --rpcapi "db,eth,net,web3,personal" --ws --wsaddr "localhost" --wsport "8545" --wsorigins "*" --identity "MyTestNode" --datadir "./data" --testnet --fast

connected to web3:

const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));

listening:

contract.events.allEvents({ fromBlock: 'latest' }, console.log)

I can't use webSokets. How I can catch contract events?
web3 - version 1.0.0-beta23

@AndreyPatseiko
Suppose it is impossible by it's nature, event should be initiated by the server and it is impossible with plain http protocol

You right. I was angry because about it any single word in the web3 documentation.
Thanks for you example - if use webSocets all work correct, like as described in web3 documentation.

Anyone having a problem with watch all events not working after about 10 minutes?

Works if "latest" is used as fromBlock param, but does not work here if the fromBlock is different from latest.

Getting the following error:
Error: CONNECTION ERROR: Couldn't connect to node on IPC.

Was just exploring this same issue. Expanding @dskvr's observations, I wrapped my event listeners in a call to web3.eth.getBlock('latest') and explicitly pass the latest block number as fromBlock. I'm doing this in docker w nodemon so it's been starting & restarting pretty wildly without any issues (so far). My setup looks something like:

web3.eth.getBlock('latest').then(res => {
  console.log(`Starting event watchers from block ${res.number}`)
  contract.events.allEvents({ fromBlock: res.number }, eventHandler)
})

Earlier I was debugging something similar and realized I only halfway enabled my websockets connection. Remember to specify websockets both while specifying the web3 provider ws://localhost:8546 and while starting geth --ws --wsorigins="*".

Same here using parity and web3 1.0.0-beta21

omg it is still not fixed since 2015 0_O

no, here is working

Sorry, forgot to close it. It works for me most of the time, but there are some things to keep in mind:

  • Only use WebSocket or IPC provider
  • If you want to listen for a bunch of events, don't use INFURA (that also means don't use MetaMask unless you add a custom node), since it's hella slow for event aggregation

Happy hacking

To expand the comments by @onbjerg I also want to suggest one more troubleshooting tip for those who find themselves here:

  • Try a different Ethereum client. If you're using parity and are having trouble fetching old logs, try geth instead.

See parity issue 6637 for details although it looks like this might have been fixed a couple days ago... Geth, on the other hand, has always been extremely reliable while fetching historical events.

Was this page helpful?
0 / 5 - 0 ratings