Subscription "newHeads" doesn't exist

Hi,

I’m trying to subscribe “newheads” with using web3 on node.js.
But Error message “Subscription “newHeads” doesn’t exist. Subscribing anyway.” is returned.
However I can get the response of newBlockHeader.

Is this behavior properly?
The message “Subscription “newHeads” doesn’t exist” confuses me.
Because I get the correct response despite the error message being sent.

My code is here.

‘use strict’;

const Web3 = require(‘web3’);

let web3 = new Web3(new Web3.providers.WebsocketProvider(‘wss://mainnet.infura.io/ws/v3/MY_ID’));

const webSocketTest = async () => {
let subscription = web3.eth.subscribe(“newHeads”, function (error, result) {})
.on(“data”, function (block) {
console.log(block);
});
};

webSocketTest();

This may be a simple question, but I’m newbie of programer.
I would appreciate it if you could reply.

node.js: v14.3.0
web3: 1.2.11

Hi @pierogi - welcome to the Infura community! The reason you’re getting that error message is that you’re attempting to subscribe to the event itself. The API lets you subscribe to an event type and then add filters. The valid event types are:

  • pendingTransactions: Receive a subset of new transactions sent to the blockchain (Primarily used for miners who want to be selective of the transactions they process)
  • newBlockHeaders: Receive a notification when a new block has been added to the blockchain.
  • syncing: Receive a notification when node syncing starts/stops
  • logs: Receive a notification on log updates on the blockchain. These are the events you’re interested in.

You can take a look at the API documentation for examples on how to use subscribe("newBlockHeaders")

Hi @Leiya_Kenney - Thank you for your reply. It can work without any error messages! Thank you!