Skip to content

  Bloq and ETC Labs introduce managed API service for Ethereum and Ethereum Classic. Learn More

The Sync Blog

Updates and insights from the Bloq team

« Blog Home

How-To: Use the Websocket API to Set Up Bitcoin / Bitcoin Cash Notifications

Bitcoin notifications and websocket

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.

Websocket Setup

Just like the Javascript SDK, Websocket API calls are written as a JS document and executed in the command line with node file_name.js The Javascript can also be embedded in .html.

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.

Room Creation

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 btc.connect.bloq.cloud to bch.connect.bloq.cloud.

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.

Next Steps

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.