-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise support for MessageIdGenerator #11
Comments
@s-ueno This is interesting feature. Here are some thoughts. I'm curious what you think about it - how this relates to your case? The feature implies that the addMessage also need to be asynchronous. This approach has two caveats: a user will not see the message until the promise is resolved and can first see the appearance of the second message and then the first message above it. This is bad UX. The second approach is to add some temporary id, insert a message to the collection immediately and replace the id when a promise is resolved. In this case, the addMessage method can be synchronous. In both approaches, we need to handle the case when the add message promise is rejected. The question is how? Looking forward to reading your thoughts. |
Hi, @supersnager I have similar thoughts to the second approach. The first approach, as you say, has a poor UX. What is interesting is that instead of adding the ID in the addMessage, we delegate the issuing of the ID to the MessageIdGenerator. This is because, apart from the process of adding a message on the UI, we delegate the issuance of the key associated with the message to the MessageIdGenerator. However, since some developers may think that issuing IDs in addMessage is sufficient, another way to think of it is to issue events when some operation is performed on a message, such as addMessage or updateMessage.
If these events can be subscribed to asynchronously, it would be possible to freely implement processes such as exchanging messages with the server via the event hub or intervening in the results with updateMessage. Callbacks can be received by This would be in line with the concept of separating the ui from the backend. PS. I like the fact that many services abstract the ui and just deliver data, whereas on the contrary, they abstract the data and logic and provide a typical UI in chat. I also particularly like the fact that it works fine with React 18. |
@s-ueno Thanks for your comment, we are going in the right direction :) If I understood you correctly, do you want to leave the message addition process as is and add an onAddMessage callback? something like this: // Generate random ID for the UI
const groupIdGenerator = () => nanoid();
const messageIdGenerator = <T extends MessageContentType>(message: ChatMessage<T>) => nanoid();
const externalStorage = new BasicStorage({ groupIdGenerator, messageIdGenerator});
// Get the id from the server and update the message
externalStorage.addEventListener("onAddMessage", async (addedMessageWithGeneratedId:ChatMessage, conversationId: ConversationId) => {
const messageIdFromServer = await fetch(...);
addedMessageWithGeneratedId:ChatMessage.id = messageIdFromServer;
externalStorage.updateMessage(addedMessageWithGeneratedId:ChatMessage);
});
<ChatProvider serviceFactory={serviceFactory} storage={externalStorage} config={{
typingThrottleTime: 250,
typingDebounceTime: 900,
debounceTyping: true,
autoDraft: AutoDraft.Save | AutoDraft.Restore
}}>
<App />
</ChatProvider> I have an additional question to understand your message flow. Additional thoughts on implementation:
|
Hi, @supersnager I wanted to persist the ID of a sent message and share it with my members. I want to be able to reference past messages with an immutable URL based on the message ID. I was confused because sendMessage in @useChat is void and the job of issuing message IDs was outsourced to MessageIdGenerator.
However, as you say, MessageIdGenerator seems to be able to achieve this by returning the issued ID as is and making the send process asynchronous.
Thank you very much for all your help. |
I am looking at BasicStorage.
We add and publish messages with addMessage and use MessageIdGenerator internally, but we sometimes ask the server to issue message IDs.
It would be helpful if it could be accepted asynchronously.
The text was updated successfully, but these errors were encountered: