I have a contract function in private network with a for loop and emitting event inside the loop. Following is the code :

pragma solidity ^0.4.20;

contract Eventsloop{

event loopidx(uint idx,uint jdx);

function watcheventloop() public payable returns(bool){

    for(uint j =0;j<5;j++){ 

    for(uint i=0;i<20;i++){

         emit loopidx(i,j);




const Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider(""));  
const abidata = //abidata;  

var deployedContract = new web3.eth.Contract(abidata,contractaddress,{
defaultAccount: account, // default from address
defaultGasPrice: '100000000000'

deployedContract.methods.watcheventloop().send({from:account,gas: 400000000})
console.log("Tx complted"+JSON.stringify(receipt))
console.log("ERror "+JSON.stringify(e))

}).on('data',event => {
console.log('new event ',event)//for each iteration an action should occur.
}).on('error', error =>{
console.log("Error ",error);

Is it possible to watch event in each iteration,so that some action could be done based on emitted values(i,j).

When tested with single looping,I was getting event log of all iteration together(iteration can go up to 500) and will be difficult to handle as another loop has to be written to fetch values from event logs in JS file. Is there any method for each iteration in contract, some action could be triggered in JS file?

Based on my requirements I need double looping but, watching event stopped in between with an error as follows:

Error: Subscriptions are not supported with the HttpProvider

However, transaction receipt of function_call displayed all eventlogs.

So then I updated code with

web3 = new Web3()
const web3 = new Web3(new Web3.providers.WebsocketProvider(''));

and got another error :


Help will be appreciated :)

  • 1
    The default websocket port is 8546. The URL should be ws:// See here ethereum.stackexchange.com/questions/11527/… for other possible issues.
    – Ismael
    Commented Jun 21, 2019 at 16:16
  • Updated it, still on a function call this error occurs.
    – angelmaria
    Commented Jul 1, 2019 at 6:53

1 Answer 1


Since all of the eventinloop events will be emitted in a single transaction, you will see the list of all events in the single calc() transaction.

There is no way to react to the first event in the loop, before the second loop is executed (as transactions are atomic, so all events are emitted in a single transaction).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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