[Ethereum] How to i setup a web3 webservice that listens for events from the smart contract

eventsnodejsweb3js

I have a smart contract that broadcasts events to web3 clients, upon each event being broadcast, the listening client makes a tx call to my contract.

I would like to set up an 'always on' event listener – so some sort of web service and all it does is listen for events being broadcast from my smart contract and make tx calls. This way the browser would not have to be open to fire off tx to my contract when it 'hears' an event.

In my mind, obviously this webservice would need to have its own local node running, and have a coinbase with enough funds in it to run the tx.

How could I accomplish this scenario?

Basically, I need a web3 file to always be listening – with no one visiting the actual page in browser that has the js listeners.

Is this achievable? Would something like nodejs be able to do this?

Please help it is crucial to my smart contract application.

Best Answer

You could certainly make a daemon that listens for the event and calls the contract. As you describe it it doesn't even need to be a web service; For example, just write the code that you'd normally have run in the browser to listen for the event then fire the contract, and run it from the command line with node. You may want to run this with upstart or god that will start it up again if it falls over for some reason.

However, is there a reason why you can't just make the contract that's currently calling the original event call the second contract directly?

Related Topic