I have a basic node express application. It connects to my locally running hardhat network via ethers & websocket. The connection seems to work, since I can query the blockchain succesfully (for instance await ethers.getBlockNumber() returns the correct block number).

However, my event listeners do not seem to work. The callbacks are never triggered.

Code example: I'm using a loader function when I initialize the express app. When it is executed, I connect to the websocket provider. Then I successfully print the current block number to the console. Then I register an event listener on the "block" event (taken from the documentation). However, the listener never fires.

I'm connecting to the hardhat network, which is configured to produce a new block every five seconds (I can see that this is the case in the hardhat console). I would expect the listener to be fired every five seconds, always when a new block is mined.

What am I missing?

import { ethers } from "ethers";

export default async ({ expressApp }) => {
  const provider = new ethers.providers.WebSocketProvider(

  console.log(await provider.getBlockNumber());

  provider.on("block", () => console.log("new block"));

  expressLoader({ app: expressApp });

EDIT: If I use the JsonRpcProviderinstead of WebSocketProviderand the following url http://localhost:8545 the callbacks are fired. What do I need to do to achieve the same behavior with websocket?

1 Answer 1


I do have the same issue.

nomiclabs has related open issue

provider.on("block", () => console.log("new block"));

Above code works via http -- because it has interval polling strategy underneath. So it is not notified via ws subscription, but requesting is there a new block.

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.