react-chat-widget icon indicating copy to clipboard operation
react-chat-widget copied to clipboard

Render messages from a database

Open ghost opened this issue 6 years ago • 5 comments

Hi!

Thanks for this awesome module.

Just wanted to ask if there was a way to have the chat window render messages that are saved in a database?

I'm saving messages in Firebase, and need the messages that were previously sent to render in the chat window when they are opened.

Is there a way to specify a source for these messages?

ghost avatar May 12 '18 16:05 ghost

Hi @davidbaek92! What you can do is use either the addResponseMessage and addUserMessage when the component that's using the widget is going to be rendered. You can use whatever source you need since that will be handled by the component using the widget. For example:

import { addResponseMessage } from 'react-chat-widget';
...

export default MyComponent extends React.Component {
  handleUserMessage = message => {
    doSomethingWithTheUserMessage(message).then(response => {
      addResponseMessage(response);
    });
  }

    render() {
      return ...
    }
}

mcallegari10 avatar May 13 '18 21:05 mcallegari10

Hi Martin!

It seems like the above code is sending a response message in the Widget every time a user sends a message? This isn't exactly what I'm looking for.

What I have is an array of messages being held in state within a React component. What I need is for the chat widget to show all the old messages that are held in state once the Widget button is clicked and the chat view is opened.

Is there any way for me to do this?

On Sun, May 13, 2018 at 5:30 PM, Martín Callegari [email protected] wrote:

Hi @davidbaek92 https://github.com/davidbaek92! What you can do is use either the addResponseMessage and addUserMessage when the component that's using the widget is going to be rendered. You can use whatever source you need since that will be handled by the component using the widget. For example:

import { addResponseMessage } from 'react-chat-widget';... export default MyComponent extends React.Component { handleUserMessage = message => { doSomethingWithTheUserMessage(message).then(response => { addResponseMessage(response); }); }

render() {
  return ...
}

}

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Wolox/react-chat-widget/issues/56#issuecomment-388657399, or mute the thread https://github.com/notifications/unsubscribe-auth/Abuo__KxC-POG02tkFN7RqBlp3iIMp7Bks5tyKXxgaJpZM4T8ehi .

ghost avatar May 15 '18 22:05 ghost

@davidbaek92 you can fetch / pre-load these messages from your database at your componentDidMount() is called, just append these data using addResponseMessage, addUserMessage, addLinkSnippet to the component as soon as the previous messages were received.

xemasiv avatar May 18 '18 07:05 xemasiv

@davidbaek92 Did we answer your question or you need more help? :slightly_smiling_face:

mcallegari10 avatar May 22 '18 01:05 mcallegari10

@davidbaek92 you can fetch / pre-load these messages from your database at your componentDidMount() is called, just append these data using addResponseMessage, addUserMessage, addLinkSnippet to the component as soon as the previous messages were received.

But it is appending all previous Chat messages too. Can you help me on this??

sev7enwonder avatar Aug 29 '20 09:08 sev7enwonder