microsoft / microsoft/BotFramework-DirectLineJS

Multiple Continuous API Requests Issue

Đang mở
#424 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
TypeScript
Star
199
Fork
133
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện hành vi với mã đăng ký React được cung cấp, đồng thời kiểm tra tần suất subscribeToBotMessages được gọi và cách các đăng ký activity$ được tạo. So sánh các yêu cầu mạng phát sinh với các lần phát của activity$ và xác nhận rằng các yêu cầu chỉ xảy ra khi nhận được tin nhắn mới, không có đăng ký trùng lặp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
react, typescript
Lĩnh vực
frontend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.