Back to Home

⚡ WebSocket API

Real-time bidirectional communication for streaming AI responses, live updates, and instant notifications.

🔌 Connection

WebSocket Endpoint

wss://ws.vigthoria.io/v1

Connecting with JavaScript

const ws = new WebSocket('wss://ws.vigthoria.io/v1'); // Authentication ws.onopen = () => { ws.send(JSON.stringify({ type: 'auth', token: 'YOUR_API_KEY' })); }; // Handle messages ws.onmessage = (event) => { const message = JSON.parse(event.data); console.log('Received:', message); }; // Handle errors ws.onerror = (error) => { console.error('WebSocket error:', error); }; // Handle disconnection ws.onclose = () => { console.log('Disconnected'); };

Connection Status

Connected
Disconnected

📨 Message Types

Authentication

SEND auth
{ "type": "auth", "token": "YOUR_API_KEY" }
RECEIVE auth_success
{ "type": "auth_success", "user_id": "user_abc123", "plan": "pro" }

Streaming Chat

SEND chat.stream

Start a streaming chat request:

{ "type": "chat.stream", "message": "Explain quantum computing", "model": "vigthoria-reasoning", "conversation_id": "conv_xyz789" }
RECEIVE chat.chunk

Receive streaming chunks:

{ "type": "chat.chunk", "content": "Quantum computing ", "done": false } // ... more chunks ... { "type": "chat.chunk", "content": "qubits.", "done": true, "tokens_used": 450 }

Generation Progress

SEND subscribe.job

Subscribe to job progress updates:

{ "type": "subscribe.job", "job_id": "job_abc123" }
RECEIVE job.progress
{ "type": "job.progress", "job_id": "job_abc123", "progress": 45, "status": "processing", "eta_seconds": 30 }
RECEIVE job.completed
{ "type": "job.completed", "job_id": "job_abc123", "output_url": "https://cdn.vigthoria.io/..." }

💬 Streaming Chat Example

const ws = new WebSocket('wss://ws.vigthoria.io/v1'); ws.onopen = () => { // Authenticate ws.send(JSON.stringify({ type: 'auth', token: API_KEY })); }; ws.onmessage = (event) => { const msg = JSON.parse(event.data); if (msg.type === 'auth_success') { // Start streaming chat ws.send(JSON.stringify({ type: 'chat.stream', message: 'Write a poem about AI', model: 'vigthoria-creative' })); } if (msg.type === 'chat.chunk') { // Append chunk to output process.stdout.write(msg.content); if (msg.done) { console.log('\\n\\nTokens used:', msg.tokens_used); } } };

🔄 Heartbeat

The server sends a ping every 30 seconds. Respond with a pong to keep the connection alive:

ws.onmessage = (event) => { const msg = JSON.parse(event.data); if (msg.type === 'ping') { ws.send(JSON.stringify({ type: 'pong' })); } };

⚙️ Features

❌ Error Handling

RECEIVE error
{ "type": "error", "code": "rate_limited", "message": "Too many requests. Please slow down.", "retry_after": 60 }

Common error codes: auth_failed, rate_limited, invalid_message, subscription_required