Our previous tutorials outlined different tools that enable users to manually submit and query data. Instead of doing this manually, you can use the Websocket API to activate a “room” that “subscribes” to the network and notifies the user with a constant stream of data as it is received. These rooms can be set to listen for and report network transactions, blocks, and address activity in real-time.
The Websocket file begins with socket initializations followed by the room specifications. This is written as follows:
The “token” required is an authentication token, which is generated through your Bloq account. (See the Getting Started guide.) From there, create a Client Access Token in your terminal with
bcl client-token and paste it into the above code.
The following code can be written as a JS file and executed in the terminal to create a room that listens to the Bitcoin chain and prints all incoming transactions. Make sure to supply your own authorization token in the header:
The above code will also report back any errors encountered and indicate when the room is disconnected from the network.
To create a Websocket room listening to the Bitcoin Cash blockchain, simply change the URL in the header from
The Websocket API additionally provides commands for users to subscribe to blocks and individual addresses. To learn more about the Websocket API please see the Bloq Connect Socket.IO Technical Reference.
The constant data stream enabled through the Websocket API provides a strong foundation for a broad range of real-time blockchain analytics tools. By filtering for specific pieces of information, deriving data from the stream, and packaging the code within web pages and larger applications, users are well-equipped to create block explorers and “Whale Alert”-like applications. Stay tuned, and we’ll show you how to do both in further tutorials.