0

I have been trying to listen to the event of my smart contract; however, I receive the error.

MyContract.events.myEvent(

function(error, event){ 
    if(!error){
        $("#instructor").html(event.args.name + ' (' + event.args.age + ' years old)');
    }else{
        console.log(error); 
    }
    
})
     

I receive the following error

 Error: The current provider doesn't support subscriptions:

Web3.js version is [email protected]

I am well aware of the following solution; however, it doesn't work on my case anyway.

My smart contract

contract Coursetro {
    
   string fName;
   uint age;
   
   event Instructor(
    string name,
    uint age 
    );
   
   function setInstructor(string _fName, uint _age) public {
       fName = _fName;
       age = _age;
       Instructor(_fName, _age);
   }
   
   function getInstructor() public constant returns (string, uint) {
       return (fName, age);
   }
    
}
2
  • which provider are you using to listen to events. you need to have WebSocket enabled provider to listen to events in Ethereum Commented Nov 19, 2020 at 6:32
  • I just gave an answer to the question.
    – Emrah
    Commented Nov 19, 2020 at 16:35

1 Answer 1

1

As @myanzik-shresth mentioned, you will need to use WebsocketProvider in order to subscribe to an event if you are using version [email protected]

Use

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

Not

web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

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.