Skip to main content

WebSocket Connection Setup

Connect to real-time financial news streams using WebSocket API. Setup Steps:
  • Configure WebSocket client with API key authentication
  • Subscribe to filtered news streams
  • Handle real-time data events
  • Implement error handling and reconnection logic

Prerequisites

WebSocket requires Pro Plan ($99/month) or Enterprise Plan. Upgrade your plan if needed.

Step 1: Get Your API Key

  1. Login to Byul API Dashboard
  2. Your API key is automatically generated and displayed
  3. Copy your API key (starts with byul_v2_)

Step 2: Install Dependencies

npm install socket.io-client

Step 3: Create Your First Connection

const { io } = require('socket.io-client');

// Replace with your actual API key
const API_KEY = 'byul_api_key';

// Connect to WebSocket
const socket = io('wss://api.byul.ai/news-v2', {
  auth: { apiKey: API_KEY }
});

// Handle connection
socket.on('connect', () => {
  console.log('Connected to Byul WebSocket');
  
  // Subscribe to high-importance news
  socket.emit('news:subscribe', {
    minImportance: 7,
    startDate: '2024-01-01T00:00:00.000Z'
  });
});


// Handle incoming news
socket.on('news:data', (response) => {
  const { news } = response.data;
  
  news.forEach(article => {
    console.log(article.title);
    console.log(`Importance: ${article.importanceScore}/10`);
    console.log(`Category: ${article.category}`);
    console.log('---');
  });
});

// Handle authentication success
socket.on('auth:success', (data) => {
  console.log('Authenticated successfully:', data.user.plan);
});

// Handle errors
socket.on('news:error', (error) => {
  console.error('Error:', error.message);
});

console.log('Connecting to Byul WebSocket...');

Step 4: Run Your Code

node your-websocket-app.js
Expected output:
Connecting to Byul WebSocket...
Connected to Byul WebSocket!
Authenticated successfully: pro
Breaking: Fed Issues Emergency Statement
Importance: 10/10
Category: fed
---

Connection Complete

WebSocket connection established and receiving real-time news updates.

What’s Next?

Authentication

Learn about API key authentication and security best practices

Filtering News

Filter news by importance, symbols, and categories

Error Handling

Handle connection errors and implement retry logic

Production Examples

See production-ready implementation examples

Troubleshooting

Common Issues

Authentication Failed
  • Verify API key format (starts with byul_v2_)
  • Check API key at Dashboard
  • Confirm Pro or Enterprise plan access
WebSocket Not Supported
  • Requires Pro or Enterprise plan
  • Check current plan status
Connection Timeout
  • Verify network connectivity
  • Check firewall settings
  • Test from different network if needed

Additional Resources

Security: Use environment variables in production to store API keys securely:
export BYUL_API_KEY=byul_api_key