# WebSocket Integration

#### 🧭 Supported Methods

Currently, CoinVera WebSocket supports the following subscription methods:

* `subscribePrice`: Subscribe to real-time price updates for tokens
* `subscribeTrade`: Subscribe to live trade activity data
* `subscribeNewpair`: Subscribe to stream new tokens & pools live

***

#### 🌐 WebSocket Endpoint

```
wss://api.coinvera.io
```

***

#### 🛠️ Required Parameters

Each subscription request must include the following parameters:

| Parameter | Type     | Description                                                                 |
| --------- | -------- | --------------------------------------------------------------------------- |
| `apiKey`  | `string` | Your CoinVera API key                                                       |
| `method`  | `string` | One of: `subscribePrice`, `subscribeTrade`                                  |
| `tokens`  | `array`  | One or more token addresses or wallet addresses (based on your plan limits) |

***

#### 💻 Code Example

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

const apiKey = ''; // Your CoinVera API Key
if (!apiKey) {
  console.error('Missing API key');
  process.exit(1);
}

// WebSocket endpoint
const WS_URL = 'wss://api.coinvera.io'; // Normal usage

// Create WebSocket client
const ws = new WebSocket(WS_URL);

let pingInterval;

// Subscribe to trade updates once the connection is open
ws.on('open', () => {
  console.log('WebSocket connection opened. Subscribing to trades...');

  const payload = {
    apiKey,
    method: 'subscribeTrade',
    tokens: [''], // Comment or remove for methods: subscribeNewpair
  };

  ws.send(JSON.stringify(payload));
  console.log('Subscribe request sent:', payload);

  // Start sending PING to keep connection alive every 10 seconds
  pingInterval = setInterval(() => {
    if (ws.readyState === WebSocket.OPEN) {
      ws.ping();
    }
  }, 10000);
});

// Handle incoming messages
ws.on('message', (data) => {
  try {
    const message = JSON.parse(data);
    console.log('Received:', message);
  } catch (err) {
    console.error('Error parsing message:', err);
  }
});

// Handle errors
ws.on('error', (err) => {
  console.error('WebSocket error:', err);
});

// Handle connection close
ws.on('close', (code, reason) => {
  console.log(`WebSocket closed: ${code} - ${reason}`);
  if (pingInterval) clearInterval(pingInterval);
});

```

***

#### 📤 Sample Response: `subscribePrice`

```javascript
{
  ca: '3VCkk4EVWQjCP8usuVK9ArfSmViFAcNcTMPivFJPpump',
  dex: 'PumpFun',
  priceInSol: '0.000000028020233436191693',
  priceInUsd: '0.000004195937910602868'
}
```

***

#### 📤 Sample Response: `subscribeTrade`

```javascript
{
  signature: '5MZFwRDa6Q9ErRetB8RvaUjepNM9RodDG1ZSfMLunSq36NcB38FhR5mBsQFvmTWjzkG8xztMnKmFXVy3eQS6fyv3',
  signer: '58FqLVkDz8Zkg5fRAinwrAnu6a2dK1TJkDg8NG6pRimE',
  dexs: [ 'Pump.fun' ],
  ca: '3VCkk4EVWQjCP8usuVK9ArfSmViFAcNcTMPivFJPpump',
  trade: 'buy',
  priceInSol: 2.801561469095688e-8,
  solAmount: -0.004950495,
  tokenAmount: 176704.85029899998,
  TokenDelta: [
    {
      mint: '3VCkk4EVWQjCP8usuVK9ArfSmViFAcNcTMPivFJPpump',
      amount: 176704.85029899998
    }
  ]
}
```

#### 📤 Sample Response: `subscribeNewpair`

```json
{
  dex: 'Meteora DlmmV2',
  signature: '5ZX2iDtPMvsGGt6uLHF2ZivcqoyPgGxHLqe1tkRADSwGHc6L5Df6vUNuetP137V6shqRY6qTrNQdfmDWsMqycjEp',
  creator: '7Loze72RNfp2t2PUtw43ajtCNeyvzeVWLpmWm8v8Ss5m',
  pool: 'kp8a7jXmCG3QNx3WssGpJGpGpaVCkFimj7ocMsB1H3Z',
  token0: 'y7DdeCMbukNQShsGbybQmsxgVANi4nhCy7m4WZSLuCb',
  token1: 'So11111111111111111111111111111111111111112'
}
```

***

#### 🔁 Connection Keep-Alive

To maintain your WebSocket session, periodically send `ping` messages to avoid disconnection.


---

# 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://docs.coinvera.io/integration/solana/websocket-integration.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.
