# ⚡ Websocket

**1. Websocket data**

Subscribe to websocket to receive all parsed data when a new block coming. No need to call API for every block.\
Just one subscription and receive all on chain data\
\
**2. Steps to connect websocket**

* Connect to endpoint \
  `wss://[chain_name]-api.txdecoder.xyz/ws`
* In default, websocket return all data
* Send optional filter to reduce CU consumption
  * **type**: protocol type ([ Listed here](https://txdecoder.gitbook.io/docs/chains-and-protocols/protocol-type) ) , "all" to receive everything
  * **protocol**: protocol name ([ Listed here ](https://txdecoder.gitbook.io/docs/chains-and-protocols/supported-protocols))
  * **action**: action name ( [Listed here](https://txdecoder.gitbook.io/docs/chains-and-protocols/protocol-type)  )
  * **pool\_id**: pool involving in the transaction
  * **participant**: address involving in the transaction
  * **token**: token involving in the transaction
  * **min\_value\_usd**: minimum usd value , number, greater than 0

```json
{
    "type": "DEX", // protocol type: all, DEX, LENDING, STAKING, ERC, STABLE_COIN, BRIDGE, RWA
    "protocol": "Uniswap V4",
    "token" : "0xA88594D404727625A9437C3f886C7643872296AE", // - token address involving in the transaction.
    "min_value_usd": 1000 // 1000 USD
}
```

* Wait to receive new data
* Response data in **User Action** format

**3. Example code**

```javascript
const WebSocket = require('ws')

const main = async () => {
  // Connect through the WebSocket proxy with authentication
  const apiKey = process.env.API_KEY
  const proxyUrl = 'wss://bsc-api.txdecoder.xyz/ws'

  const ws = new WebSocket(proxyUrl, {
    headers: {
      'x-api-key': apiKey,
    },
    rejectUnauthorized: false,
  })

  ws.on('open', () => {
    console.log('Connected to BSC WebSocket server')

    // Send the DEX message after connection is established
    // const message = { type: 'DEX' }
    // ws.send(JSON.stringify(message))
    // console.log('Sent message:', message)
  })

  ws.on('message', (data) => {
    try {
      const message = JSON.parse(data)
      console.log('Received message:', message)
    } catch (error) {
      console.log('Received raw data:', data.toString())
    }
  })

  ws.on('error', (error) => {
    console.error('WebSocket error:', error)
  })

  ws.on('close', (code, reason) => {
    console.log(`Connection closed. Code: ${code}, Reason: ${reason}`)
  })

  // Handle graceful shutdown
  process.on('SIGINT', () => {
    console.log('Closing WebSocket connection...')
    ws.close()
    process.exit(0)
  })
}

main().catch(console.error)

```

**4. Limitation**

* Maximum 50 concurrent connections per account
* block\_number in websocket may not sequence , later block can come first, please take a note


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://txdecoder.gitbook.io/docs/api-documentations/websocket.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
