# Creating DEX New Listing streams

## 1. Use case

* New Listing dashboard, telegram bot

## 2. Steps

* Connect to endpoint `wss://[chain_name]-api.txdecoder.xyz/ws`
* Send optional parameters to filter only DEX action
  * **type**: <kbd>DEX</kbd>&#x20;
  * **action**: <kbd>initialize\_pool</kbd>

```
{
    "type": "DEX",
    "action": "initialize_pool"
}
```

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

&#x20;

## 3. Example Code (Javascript)

```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 TxDecoder WebSocket server')

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

  ws.on('message', (data) => {
    try {
      const message = JSON.parse(data)
      
        for (const txHash in message) {
            const userActions = message[txHash]
            if (!Array.isArray(userActions)) continue
            for (const userAction of userActions) {
                const { tokens, participants, tx_hash: txHash, action } = userAction
                if (!txHash) continue
                if (action !== 'initialize_pool') continue

                // TODO: process new pool here
            }
        }
        
    } 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)
```

<br>
