# Connect BTC / BCH Socket.IO API

The WebSocket API uses Socket.IO and provides access to events including:

  • Address events
  • Block events
  • Transaction events

The following examples are using BTC mainnet. Please see the supported chains and networks.

# Authentication

The Connect Websocket API requires authentication. To authorize a request, an access token must be sent in the connection URL as a querystring.

Javascript example using Socket.IO:

const io = require('socket.io-client')

const socket = io('https://btc.connect.bloq.cloud/', {
  query: {
    token: 'xxxxxx.xxxxxxxxxx.xxxxxx'
  }
})

To learn how to create an Access Token to use the websocket interface follow these instructions.

# API

This BTC API implements Socket.IO, a library that enables real-time, bidirectional and event-based communication between the browser and the server

# URL

The connection URL to interact with the Socket.IO interface is https://btc.connect.bloq.cloud.

# Rooms

  • inv: Subscribe to this room to be able to listen transactions and blocks events
  • bitcoind/addresstxid: Subscribe to this room to be able to listen to address events

# Events

Event Description
tx New transaction received from the network. This event is published in the inv room
block New block received from the network. This event is published in the inv room
<bitcoinAddress> New transaction received from network. This event is published in the \<bitcoinAddress\> room

WARNING

If a user does not subscribe to any events (address or tx + blocks), the connection will be closed after a period of time. The connection time is still billed to the user's Bloq Connect plan, so make sure to close your connection when not in use!

# Code sample: inv Room

const io = require('socket.io-client')

const socket = io('https://btc.connect.bloq.cloud/', {
  query: {
    token: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9....'
  }
})
    
socket.on('connect', function() {
      // Join the room
      socket.emit('subscribe', 'inv')
})
    
socket.on('tx', function(tx) {
      console.log('New transaction received: ', tx)
})
    
socket.on('block', function(block) {
      console.log('New block received: ', block)
})

socket.on('error', function(err) {
      console.error('Error: ', err)
})

socket.on('disconnect', function(reason) {
      console.error('Disconnect, reason: ', reason)
})

# Code sample: bitcoinAddress Room

const io = require('socket.io-client')

const socket = io('https://btc.connect.bloq.cloud/', {
  query: {
    token: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9....'
  }
})

socket.on('connect', function() {
      // Join the room
      socket.emit('subscribe', 'bitcoind/addresstxid', [
        '1CK6KHY6MHgYvmRQ4PAafKYDrg1ejbH1cE',
        '1AJbBmAXSQ23WiAiynScedqCu4mQ3ENPox'
      ])
})

socket.on('error', function(err) {
      console.error('Error: ', err)
})

socket.on('disconnect', function(reason) {
      console.error('Disconnect, reason: ', reason)
})
Last updated: 5/18/2020, 9:49:19 PM