Listening to New Blocks⚓︎
The blocks WS WebSocket Channel sends a real-time notification every time a new block is
added to the chain.
Compared to polling the /chain/height GET endpoint, WebSockets push updates as they happen without the overhead of
repeated API calls.
This tutorial shows how to subscribe to the channel and display each update as it arrives.
Polling alternative
For a polling-based approach, see the Querying Chain and Irreversible Height tutorial.
Prerequisites⚓︎
NEM serves WebSockets using the STOMP messaging protocol over SockJS, so a STOMP client and a WebSocket transport are required.
See the WebSocket reference for details on the connection protocol.
Full Code⚓︎
Note
There is no SockJS client library for Python, so a few small helper methods are defined at the top of the file for convenience.
The snippet uses the NODE_URL environment variable to set the NEM node.
If no value is provided, a default one is used.
WS_URL defines the WebSocket endpoint for the same node.
It is derived from NODE_URL by replacing port 7890, the default HTTP API port, with 7778, the default NIS
WebSocket port.
The program runs until interrupted with Ctrl+C, which triggers the unsubscribe step before closing the connection.
Code Explanation⚓︎
Connecting to the WebSocket⚓︎
The first step is to open a connection to the node's /w/messages endpoint and start a STOMP session
over it.
Subscribing to the Channel⚓︎
The code subscribes to the blocks WS channel.
The node then notifies subscribers every time a new block is added to the chain (approximately every minute).
Notifications are not always evenly spaced. When the node adds several blocks at once, for example while catching up with its peers, the channel delivers them in a quick burst, still one notification per block.
The subscription is given an id (id-0) which is used to unsubscribe on exit.
Each incoming message is then passed to the formatting logic below.
Formatting the Message⚓︎
The body of each incoming message is the new block, following the Block schema.
For each message, the snippet prints two of its fields:
height: The height of the new block.signer: The public key of the harvester account that produced the block.
A NEM block does not include its own hash in this payload, so this tutorial identifies each block by its height
and also prints the harvester's signer.
New blocks are not yet final
New blocks are already part of the chain but not yet irreversible. Until enough subsequent blocks surpass the rewrite limit, rollbacks are still possible. After a rollback, the channel can even report a block with a lower height than one already received, because the incoming blocks replace blocks that were reported earlier.
The Querying Chain and Irreversible Height tutorial shows how to calculate the irreversible height.
Unsubscribing on Exit⚓︎
When the program is interrupted (Ctrl+C), the code unsubscribes from the channel and ends the STOMP session before
closing the connection.
This ensures a clean disconnection from the node.
Output⚓︎
The following output shows a typical run listening to new blocks:
The output shows:
- Connection (line 2): The STOMP session is established over the node's WebSocket endpoint at port
7778. - Subscription (line 3): The
/blockschannel is subscribed. - New blocks (lines 4-8): New block notifications arrive approximately every minute.
- Unsubscribe (line 9): On
Ctrl+C, the code unsubscribes and disconnects.
Conclusion⚓︎
This tutorial showed how to:
| Step | Related documentation |
|---|---|
| Subscribe to the block channel | blocks WS |
| Format block messages | Block |