microsoft / microsoft/BotFramework-DirectLineJS

Multiple Continuous API Requests Issue

Open
#424 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
199
Forks
133
PR merge metrics
No merged PRs in 30d

Description

I have created a WebChat UI in React using the botframework-directlinejs package. While subscribing to messages, the application is continuously sending multiple API requests. This is leading to performance issues and unnecessary load on the server.

Expected Behavior:
The application should only send API requests when necessary, such as when new messages are received.

Actual Behavior:
The application is continuously sending multiple API requests, as shown in the attached screenshot.

Screenshot 2024-06-26   @@165840

my code :
const subscribeToBotMessages = useCallback(() => {
if (!directLine) return;

directLine.connectionStatus$.subscribe((status) => {
  if (status === 2 && !conversationId) {
    localStorage.setItem("conversationId", directLine.conversationId);
    localStorage.setItem("token", directLine.token);
    localStorage.setItem("watermark", directLine.watermark);
  }
});

directLine.activity$
  .filter(
    (activity) =>
      activity.type === "message" && activity.from.id !== "myUserId"
  )
  .subscribe((message) => {
    const timestamp = new Date().toLocaleTimeString([], {
      hour: "2-digit",
      minute: "2-digit",
    });

    const newBotMessage = {
      from: "bot",
      botMessage: message,
      timestamp,
      liked: false,
      disliked: false,
      attachment:
        message.attachments && message.attachments.length > 0
          ? message.attachments.length === 1 && message.attachments[0]
          : null,
      carousel:
        message.attachments && message.attachments.length > 0
          ? message.attachments.length > 1 && message.attachments
          : null,
    };
    setLoading(false);
    if (message.text) {
      try {
        receiveAudioRef.current.play();
      } catch (error) {
        console.log("failed to play", error);
      }
    }
    setMessages((prevMessages) => [...prevMessages, newBotMessage]);
  });

directLine.activity$
  .filter(
    (activity) =>
      activity.type === "message" && activity.from.id === "myUserId"
  )
  .subscribe((message) => {
    const timestamp = new Date().toLocaleTimeString([], {
      hour: "2-digit",
      minute: "2-digit",
    });

    const newUserMessage = {
      from: "user",
      userMessage: message,
      timestamp,
      attachment: filePreview,
    };
    setMessages((prevMessages) => {
      // Check if the user message already exists
      if (
        prevMessages.some(
          (msg) => msg.timestamp === timestamp && msg.from === "user"
        )
      ) {
        return prevMessages;
      }
      return [...prevMessages, newUserMessage];
    });
  });

}, [directLine, conversationId]);

@billba

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the supplied React subscription code and inspect how often subscribeToBotMessages is invoked and how activity$ subscriptions are created. Compare the resulting network requests with activity$ emissions and confirm that requests occur only when new messages are received, without duplicate subscriptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.