0

I am using web socket to get event. sending transaction works fine. but event is not fired.

also I referred Web3 events not working

but it does not work, too.

I used eventemitter also. but it did not work, too. I think there is a problem '.on'. Is there any similar case to me ?

My solidity code is:

event MyEvent(...);

function MyFunction(...) public {
   ...
   emit MyEvent(...);
}

My node.js code is :

const events = require('events');
const WSS = 'ws://localhost:8881';
const provider = new Web3.providers.WebsocketProvider(WSS);
const web3 = new Web3(provider);
const eventEmitter = new events.EventEmitter();

myContract.events.MyEvent({
    //filter: {myIndexedParam: [20,23]},
    fromBlock: 0,
    toBlock: 'latest'
});

eventEmitter.on('data', e => {
    console.log(e.returnValues);
});

eventEmitter.on('error', e => {
    console.error(e);
});

// ...
// sending transaction
// ...
0

You are listening on an a wrong emitter. myContract.events.MyEvent returns an EventEmitter you have to listen to this. Check the documentation here https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#contract-events

You can try this method if what is explained in the documentation is not working.

const event = myContract.allEvents();

event.on('data', e => {
    console.log(e.returnValues);
});

//send transaction here

Also your myContract variable should hold the contract instance. see here

  • Even I tried your method, but it does not work. I think my code can't get event when it is fired. – xiaofo Jun 13 at 4:30
  • console.log the output of your transaction and check if there is an event property in that object – Sanjay S B Jun 13 at 4:39
  • myContract holds the exact contract instance. and my code is below: var event = MyContract.events.allEvents(); event.on('data', e => { console.log(e.returnValues); }); but it does not work yet. is there any necessary setting flag when I start geth to fire event ? – xiaofo Jun 13 at 4:51
  • What is the output of the transaction? The transaction where you call the function that fires the event. – Sanjay S B Jun 13 at 4:53
  • Ah. I inquired the transaction receipt. contract address is null and there is no data in 'log' attribute. I think I should correct them out first. – xiaofo Jun 13 at 4:59

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.