Truffle Contract – How to Subscribe to Specific Event

eventstruffletruffle-contract

I am using truffle-contract for connecting between frontend and smart contract. Currently, I can call method on smart contract. But now I want to subscribe to certain events on smart contract. How can I do this.

Thanks

Best Answer

Event subscription is a new feature available from web3 1.0 beta. And now, truffle-contract's upgrade to web3 1.0 is available as an experimental nightly build. So if you are not in production and feel adventurous, you can try poking around.

If you are in production, on the other hand, you may use web3 1.0 beta directly without truffle-contract's abstraction. However, since major providers like MetaMask don't support web3 1.0 subscription yet(web3.js v.1.0 deprecated HttpProvider in favor of WebSocketProvider), if you'd like to use it, you may need to setup your custom WebSocket provider for now.

If all these are not your viable options, you can still manually process the events provided in logs.

Related Topic