might be something wrong with callObject.sendAppMessage()

Options

I ma using Daily 0.37. I am using this below code to send messages.
when I console callObject, I can see that callObject is surely not null. but the error keeps throwing saying "cannot read properties of null, reading sendAppMessage" as soon as the user joins the class. I've also added check to execute the code only after the user has joined the meeting.


Answers

  • Tamara
    Tamara Moderator, Dailynista admin
    Options

    Hi @svm_agilis thanks for sharing this. Could you share a larger code snippet with us to investigate this, in something like glitch.com?

    Here is some feedback from our engineers:
    If callObject is indeed not null, you wouldn’t need the? .
    In JavasScript, if 
    callObject?.meetingState() was fine than that does not necessarily mean callObject won’t be null on the next line. Here is an example of what that code would look like:

    if (!callObject) {
        return; // Or throw an error
    }
    
    if (!peer) {
        return; // Or throw an error
    }
    
    
    if (callObject.meetingState() === MEETING_STATE_JOINED) {
        callObject.sendAppMessage(
            { message: { type: 'avatar-state-request' } }, 
            peer.sessionId
        )
    }
    

  • svm_agilis
    Options

    Hi @Tamara
    I use pretty much the same code structure that you provided still getting that issue

  • jameshush
    jameshush Dailynista
    edited June 2023
    Options

    Hi @svm_agilis I forked a previous glitch demo and can't seem to recreate the issue 🤔 A few minor suggestions:

    • Instead of using callObject.meetingState() === MEETING_STATE_JOINED I recommend listening to the participant-joined event and executing the related code there (as I did in the demo above)
    • I noticed you're doing peer.sessionId. I'm unsure if you renamed the variable by accident, but the session id variable we pass through is session_id (snake case instead of camel case).
    • If you want to save a few characters, you could pass the message as { type: 'avatar-state-request' } instead of { message: { type: 'avatar-state-request' } }. We use message in the docs to show how you would send a chat message.

    Hopefully, this helps! Otherwise, if you could fork the glitch demo recreating the issue, that would help even more!

  • svm_agilis
    Options

    Hi @jameshush

    I am using this below code, I am getting that issue randomly and always when running playwright tests in headless mode. i am getting this issue when I invoke the function on joined-meeting event. if set timout for 6 secs then i am not facing the issue.
    const sendMessageAfterJoining = useCallback(() => {

    if (!callObject) { return }

    if (callObject?.meetingState() == MEETING_STATE_JOINED) {

    const stateObj = {}

    stateObj[getId] = isAvatarVisible

    callObject?.sendAppMessage(

    { message: {

    type: 'avatar-state-update',

    value: stateObj

    },

    }, '*' );

    }

    }, [callObject, isAvatarVisible, getId])


    useEffect(() => {
    if (!callObject) { return }
    callObject.on('joined-meeting', sendMessageAfterJoining)

    return () => {
    callObject.off('joined-meeting', sendMessageAfterJoining)

    }

    }, [callObject, sendMessageAfterJoining])

    Thank you

  • svm_agilis
    Options

    Hi
    I found this issue occurs when participants try to send messages immmediately after they join the call. At present I can fix the issue by using setTimeout for 3-4 secs. I hope Daily team can easily fix this issue with the info that I've provided.

  • jameshush
    jameshush Dailynista
    Options

    Hi @svm_agilis , I'd suggest trying to use daily-react here and simplifying your code a bit, something like this should work:

      const sendAppMessage = useAppMessage({
        onAppMessage: useCallback((ev) => {
          console.log("—- app-message: ", ev);
        }, []),
      });
    
      const sendMessageAfterJoining = useCallback(() => {
        if (!callObject) {
          return;
        }
    
        const isAvatarVisible = true;
        const getId = "avatarId";
        const stateObj = { avatarId: {} };
    
        stateObj[getId] = isAvatarVisible;
    
        sendAppMessage(
          {
            message: {
              type: "avatar-state-update",
              value: stateObj,
            },
          },
          "*"
        );
      }, [callObject, isAvatarVisible, getId]);
    
      useDailyEvent("joined-meeting", sendMessageAfterJoining);
    

    joined-meeting is only called when the local user joins the meeting. If you want to fire this when remote users join the meeting, also add this line:

    useDailyEvent("participant-joined", sendMessageAfterJoining);