How to know A new block has been added in a block chain in NodeJs

I want to listen for a transactions on nodeJs. how to determine a new block has been added in Chain through NodeJs and rpc-node.

There is no subscription API currently supported by the NodeJS SDK.

You have to poll. Use GetConsensusInfo to watch (e.g., by polling every 0.5s) for new finalized block, then use GetBlocksAtHeight to get block hashes by increasing height. And then GetBlockSummary to get the content of a block.

In GRPC V2 API there is a way to subscribe to new finalized blocks, but the nodeJS SDK is still using the older V1 API, which does not support this.

Can you expand on the V2 comment?

The node has a new V2 API since the last release.

This is the new service description concordium-grpc-api/service.proto at main · Concordium/concordium-grpc-api · GitHub with reasonable documentation.

The Rust SDK has been updated to the new API, but the rest have not yet been updated.

This new API in particular has an endpoint GetFinalizedBlocks which can be used for new finalized blocks.

Is that what you were asking? The release notes should have the details on how to enable the new API, and our distributions by default have it enabled.

How would I be able to use this v2 api from concordium-client? Is the last version v2 compatible?

Concordium-client is not yet migrated to the new API.

You can use the new API from the Rust SDK at the moment. The nodeJS SDK will be updated next.