SDK ClientMessage, message object is returning dou...
# support
n
New here, first time poster, love the SDK... two related questions which both are about the message object... 1) Why am I getting double responses/events from the message object? For instance, with this code, I'm getting the following response twice about the final transcript....
Copy code
vapi.on("message", (message) => {
      if (message.transcriptType == "final") {
          if (message.role == "assistant") {
            setConversationLog(conversationLog => [...conversationLog, { question, answer: message.transcript }]);
            console.log(message);
          }
      }
    });
Copy code
{type: 'transcript', role: 'assistant', transcriptType: 'final', transcript: 'Hi there. What question matters most to you now?'}
role: "assistant"
transcript: "Hi there. What question matters most to you now?"
transcriptType: "final"
type: "transcript"
[[Prototype]]: Object
2) If I send an additional message containing background information using vapi.send, shouldn't I get a response back in the message object thingy? I'm getting nothing back, but I want to know if and when my message gets received. Again, here's sample code...
Copy code
vapi.on("call-start", () => {
        vapi.send({
          type: "add-message",
          message: {
            role: "system",
            content: conversationString,
          },
     });
    });
    vapi.on("message", (message) => {
      if (message.type == "conversation-update") {
        console.log("Received message:", message);
      }
     });
v
@Nep Hello and welcome! I'm glad to hear you're enjoying the SDK. Let's address your questions: 1) Regarding the double responses/events from the message object, this could be happening due to multiple instances of the event listener being registered, or the event being triggered multiple times by the backend. To resolve this, ensure that you are not adding the event listener more than once in your code. Additionally, check if the backend is designed to send the message only once upon completion of speech. If the issue persists, please ask support for more info on this. 2) When you send additional information using
vapi.send
, you should indeed expect a response back in the message object. If you're not receiving any, it could be due to the message not being processed correctly by the server or an issue with the event listener not being triggered. Make sure that the message type you're listening for (
"conversation-update"
) matches the type of message the server sends as a response. If the server sends a different type, you'll need to listen for that specific type instead. If you've confirmed that the message type is correct and you're still not receiving a response, please ask support for more info on this. For both issues, it's also a good idea to check the network activity using browser developer tools to ensure that messages are being sent and received as expected. If you need further assistance, don't hesitate to reach out to support.
You can continue this conversation by mentioning me in the message.
s
@Shubham Bajaj can you please help him out?
s
@Nep sorry for the delayed response. 1. This is perceived output of console environment displaying object data for readability. So, this is not two separate logs, but rather a single object being displayed in an expanded format. To confirm: Use
console.log(JSON.stringify(message))
to see a single string representation of the object. 2. No you won't be getting a message back for background message.
n
1. Great! 2. How, then, do you recommend I confirm in code that the background context message was received?
Actually, having tested it, that's not why this is happening. For instance, this code prints out the response twice: vapi.on("call-start", () => { console.log('Starting call'); }); Perhaps this isn't a Vapi issue?
s
Hey I checked this is not happening for me, if it continues do let us know. we will resolve it.
n
Thanks.