I'm trying to set up an event in Solidity and listen to it from Web3 but I keep getting a null response. Here's my code:

The relevant part of the contract:

event NewUser(string userid);

function createNewUser(string memory _userId) public {
    // creating the user and setting it to a mapping 
    emit NewUser(_userId);

The Javascript, from Ethereum's reference:

async function watchEvents(contract) {
       fromBlock: 0
    },function(error, event){ console.log('all events?'); console.log(event); })
  .on('data', function(event){
     console.log(event); // same results as the optional callback above
  .on('changed', function(event){
     // remove event from local database
  .on('error', console.error);

Using another javascript function I can see that new users are being created, and the transactions are listed as successful on Ganache. But the watchEvents method keeps printing null.

What am I missing?

  • Are you using websocket provider? Http provider does not support events – Sanjay S B Aug 8 '19 at 6:32
  • @SanjaySB Not even for listing past data?? Anyway no, I didn't know that. Do you know how do I specify websockets in truffle and point it to ganache? If you write it as an answer and it works it'd be great. – Eddy Aug 8 '19 at 7:04

You can try out web sockets by adding the following in truffle-config.js file.

networks: {
 development: {
      provider: function() {
        return new Web3.providers.WebsocketProvider("ws://localhost:7545");
      network_id: "5777"

You will have to import Web3.

| improve this answer | |

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.