Português (BR)
cURL
curl --request GET \ --url https://api.example.com/backend/conversations/stream
Conexão SSE para atualizações em tempo real
const eventSource = new EventSource( 'https://api.leavo.ai/backend/conversations/stream', { headers: { 'Authorization': 'Bearer sua_chave_aqui' } } ); eventSource.onmessage = (event) => { const data = JSON.parse(event.data); console.log('Nova atualização:', data); }; eventSource.onerror = (error) => { console.error('Erro no stream:', error); };
new_message
{ "type": "new_message", "data": { "lead_id": "uuid", "message": { "id": "uuid", "content": "Olá!", "role": "user", "timestamp": "2024-01-15T10:30:00Z" } } }
conversation_update
{ "type": "conversation_update", "data": { "lead_id": "uuid", "is_ai_active": false, "unread_count": 0 } }
typing
{ "type": "typing", "data": { "lead_id": "uuid", "is_typing": true } }
class ConversationStream { constructor(apiKey) { this.apiKey = apiKey; this.eventSource = null; this.listeners = new Map(); } connect() { this.eventSource = new EventSource( 'https://api.leavo.ai/backend/conversations/stream', { headers: { 'Authorization': `Bearer ${this.apiKey}` } } ); this.eventSource.onmessage = (event) => { const data = JSON.parse(event.data); this.emit(data.type, data.data); }; this.eventSource.onerror = (error) => { console.error('Stream error:', error); // Reconectar após 5 segundos setTimeout(() => this.connect(), 5000); }; } on(eventType, callback) { if (!this.listeners.has(eventType)) { this.listeners.set(eventType, []); } this.listeners.get(eventType).push(callback); } emit(eventType, data) { const callbacks = this.listeners.get(eventType) || []; callbacks.forEach(cb => cb(data)); } disconnect() { if (this.eventSource) { this.eventSource.close(); this.eventSource = null; } } } // Uso const stream = new ConversationStream('sua_chave_aqui'); stream.on('new_message', (data) => { console.log('Nova mensagem de', data.lead_id); updateChatUI(data.message); }); stream.on('typing', (data) => { showTypingIndicator(data.lead_id, data.is_typing); }); stream.connect();