Streaming Call Issue Connecting to Stream Url
# support
m
Hello! I've been trying to listen to the call as it happens and tried streaming using the example you have in your documentation but I keep getting a connection error: WebSocket connection to 'wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/c7a5ace5-86fb-4713-b44f-47f3836f4577/transport' failed: Here's most of the code for it: // Connect to WebSocket if streaming is enabled if (enableLiveStreaming && data.listenUrl) { try { // Initialize audio context before connecting await initAudioContext(); // Function to attempt WebSocket connection const connectWebSocket = async (url: string, attempt: number = 1) => { console.log(
Attempting WebSocket connection (attempt ${attempt})...
); const ws = new WebSocket(url); websocketRef.current = ws; ws.binaryType = 'arraybuffer'; return new Promise((resolve, reject) => { const timeout = setTimeout(() => { if (ws.readyState !== WebSocket.OPEN) { ws.close(); reject(new Error('Connection timeout')); } }, 5000); ws.onopen = () => { clearTimeout(timeout); resolve(ws); }; ws.onerror = () => { clearTimeout(timeout); reject(new Error('Connection failed')); }; }); };
v
@Mistermanu#000 Hello there! It seems like you're on the right track with setting up live streaming for your calls. The WebSocket connection error you're encountering could be due to a few different reasons, such as network issues, incorrect URL, or server-side problems. Here are a few steps you can take to troubleshoot the issue: 1. **Check the WebSocket URL**: Ensure that the WebSocket URL you're trying to connect to is correct and that you have the necessary permissions to access it. 2. **Network Configuration**: Verify that your network allows WebSocket connections and that there are no firewalls or proxies blocking the connection. 3. **Server Status**: Check if the server you're trying to connect to is up and running. You can try to connect to the WebSocket URL from a different network or device to rule out local network issues. 4. **Retry Logic**: Implementing retry logic can help in case of intermittent connectivity issues. You already have an attempt counter in your function, which is good. You might want to add a delay between retries and limit the number of retries to prevent endless loops. 5. **Error Handling**: Enhance the error handling in the
ws.onerror
event to log or display more detailed information about the error. This can help you pinpoint the exact cause of the connection failure. 6. **Consult Documentation**: Double-check the VAPI documentation to ensure that you're following all the required steps for setting up the WebSocket connection. 7. **Contact Support**: If you've gone through all the above steps and still face issues, it might be best to reach out to VAPI support for further assistance. If you'd like, I can assist you with implementing any of these steps or provide more detailed guidance on a specific part of the process. Just let me know how I can help!
You can continue this conversation by mentioning me in the message.
m
@Vapi I tried testing the URL in a websocket test site: https://piehost.com/websocket-tester and got the same error. The URL is straight from VAPI listenUrl property so I don't see why there would be a connection error?
v
@Mistermanu#000 If you're receiving the same connection error even when using a WebSocket test site, it's possible that the issue might not be with your code but rather with the WebSocket service or the URL itself. Here are a few additional steps you can take: 1. **Validate the URL**: Ensure that the
listenUrl
you received from VAPI is valid and has not expired. Some WebSocket URLs are only valid for a short period of time after generation. 2. **Check for Service Outages**: There might be a temporary service outage or maintenance on the VAPI side that is causing the WebSocket connection to fail. You can check the service status or reach out to VAPI support to confirm this. 3. **Authentication and Protocols**: Verify if there are any additional authentication steps or specific protocols required by VAPI that you need to follow when establishing a WebSocket connection. 4. **Contact VAPI Support**: Since the URL is provided by VAPI and you're following the correct procedure, it would be best to contact VAPI support for assistance. They can provide insights into whether there's an issue with the service or if there's something specific about your account or setup that needs to be addressed. Remember to provide VAPI support with all the relevant details, including the exact error message, steps you've taken to troubleshoot, and any other pertinent information that could help them diagnose the issue.
m
@User Hey guys! any ideas as to why the listenUrl wouldn't be working during an active call (connection error)? Thanks!
s
@Mistermanu#000 It seems like your WebSocket connection is failing. The issue could be a network restriction (like a firewall) or something else. Before diving into your code, let’s make sure the listenUrl is valid and hasn’t expired. It should look like this: wss:////transport
Verify the listenUrl Use a WebSocket client like wscat to test the URL directly. If it works, the problem is probably in your code or local environment. If it doesn’t connect, check if the URL is still valid, or regenerate it.
Copy code
sh
wscat -c wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/<call-id>/transport
@Mistermanu#000 Do let me know How it goes!!
m
@Shubham Bajaj The URL I got was from the create call response and was exactly as such: wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/c7a5ace5-86fb-4713-b44f-47f3836f4577/transport I tested it on https://piehost.com/websocket-tester while the call was still happening and they also showed a connection error. Not sure why that would be the case, am I supposed to setup AWS storage in the VAPI dashboard for the streaming URLs to work?
s
@Mistermanu#000 it's working for me I just tested it out. Your doing something wrong, most likely you trying to use it for terminated call.
Copy code
ts
import { WebSocket } from "ws";
import * as fs from "fs";

let pcmBuffer = Buffer.alloc(0);

const ws = new WebSocket(
  "wss://phone-call-websocket.aws-us-west-2-backend-production2.vapi.ai/5c5e5--004e7/listen",
);

ws.on("open", () => console.log("WebSocket connection established"));

ws.on("message", (data: any, isBinary: any) => {
  // console.log({ isBinary, data });
  if (isBinary) {
    pcmBuffer = Buffer.concat([pcmBuffer, data]);
    console.log(`Received PCM data, buffer size: ${pcmBuffer.length}`);
  } else {
    console.log("Received message:", JSON.parse(data.toString()));
  }
});

ws.on("close", () => {
  if (pcmBuffer.length > 0) {
    fs.writeFileSync("audio.pcm", pcmBuffer);
    console.log("Audio data saved to audio.pcm");
  }
});

ws.on("error", (error: any) => {
  console.error({ error });
  console.error("WebSocket error:", error);
});
@Mistermanu#000 can you share your code snippet and development environment details.
m
@Shubham Bajaj Hello! Sorry for the late I was away for a while. I figured out that on your documentation was showing this: "monitor": { "listenUrl": "wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/7420f27a-30fd-4f49-a995-5549ae7cc00d/transport", "controlUrl": "" } but the response actually uses a different form of urls: wss://phone-call-websocket.aws-us-west-2-backend-production2.vapi.ai/ my code was hardcoding part of the url and just passing the call ID but I realize that was bad and I should just grab the listenURL directly. Thanks for your help!
3 Views