#Is there a way I can map websocket responses to their request, possibly by passing in a requestId?
1 messages · Page 1 of 1 (latest)
AI-generated Answer for michael_18970
Yes, you can map WebSocket responses to their requests by including a unique identifier, such as a requestId, in your messages. This way, you can track which response corresponds to which request. Here's a basic example of how you might implement this in JavaScript:
const ws = new WebSocket('wss://your-websocket-endpoint');
ws.onopen = () => {
const message = {
requestId: 'unique-request-id-123',
text: 'Your text here',
voice_settings: { /* your voice settings */ },
xi_api_key: 'your-api-key'
};
ws.send(JSON.stringify(message));
};
ws.onmessage = (event) => {
const response = JSON.parse(event.data);
if (response.requestId === 'unique-request-id-123') {
// Handle the response for this specific request
console.log('Received response for requestId:', response.requestId);
}
};
In this example, each message sent to the WebSocket includes a requestId. When a response is received, you can check the requestId to determine which request it corresponds to. This approach helps in managing multiple requests and their respective responses efficiently.
Really? That's not in the documentation