react-chatbot-kit icon indicating copy to clipboard operation
react-chatbot-kit copied to clipboard

How can I update the existing message?

Open bushev opened this issue 2 years ago • 3 comments

Hi,

I am in the process of integrating this library with a WebSocket server that broadcasts messages partially. Consequently, whenever I receive an extension of a message (identified by a specific ID), it is necessary to update the corresponding message in the chat.

I previously attempted to substitute the entire message, but this occasionally caused the message to be divided into two to four separate segments.

image

bushev avatar Mar 17 '23 21:03 bushev

Yeah would also like to use server sent events for this @FredrikOseberg

sam-h-bean avatar Apr 02 '23 21:04 sam-h-bean

@bushev I was able to do this by doing the following:

messageBuffer += new TextDecoder("utf-8").decode(value)
var botMessage = createChatBotMessage(messageBuffer)

setState((prev: any) => {
  const prevMessagesWithoutLast = prev.messages.slice(0, -1)
  return {
    ...prev,
    messages: [...prevMessagesWithoutLast, botMessage],
  }
})

in my situation, i got the response as streaming response from fastapi (python).

zoetaka38 avatar Jun 24 '23 16:06 zoetaka38

Try this out:

const updateLastMessage = (message) => {
    setState((prev) => {
      return { ...prev, messages: [...prev.messages.slice(0, -1), { ...prev.messages.at(-1), message }]};
    });
  };

OliverThomas2000 avatar Jan 09 '24 10:01 OliverThomas2000