Message duplication issue

message observer method calling multiple times after sending message which is causing message duplication issue but after calling message list API again we are getting proper messages but during sending message we are facing this issue

Hello, we have been facing the issue of duplicate messages from past 2-3 months and have reported this issue earlier as well. Whenever we type the message it gets send duplicate times. Can you please check and give us a proper solution to it. We are soon going to launch the product and cannot take the product live with such issue as it would create a very bad impression on the user.

Hello, could you please point us out where is the previous topic you have raised, so we can check what solution our team has given to you?

Also, please help share your code and current version please :pray:

	useEffect(
		() =>
			observeMessages(channelId, {
				onEvent: (
					action: MessageAction,
					message: Amity.Snapshot<Amity.Message>
				) => {
					console.log('message OBserver Callds,ld,s>?:::::::');
					if(messages.filter((item) => item.id === message.data?.metadata.data.id)?.length > 0) {
						return;
					}
					if (action === 'onCreate') {
						console.log('message>?:::::::', JSON.stringify(message), action);
						//dispatch({notifyOnNewMessageSent(message.data)});
						if (message.data?.type == 'image' && message.data?.editedAt) {
							setMessages(prevMessages => [
								message.data?.metadata.data!,
								...prevMessages
							]);
						} else if (
							message.data?.type !== 'image' &&
							message.data?.editedAt
						) {
							setMessages(prevMessages => [
								message.data?.metadata.data!,
								...prevMessages
							]);
						}
						dispatch(notifyOnNewMessageSend(message));
					}
				}
			}),
		[channelId, messages]
	);

please find code snippet here and I have attached screen shot as well

package versions:-
@amityco/react-native-formdata-polyfill”: “^1.1.5”,
@amityco/ts-sdk”: “^0.0.1-beta.21”,
Screenshot 2023-01-03 at 16.54.05

Hey @harshit we cannot reproduce this issue on our end, however, our team has checked your code and suggested removing the ‘messages’ from this line [channelId, messages]

Please let us know if it still doesn’t work for you.

facing the same issue after remove it

Please see our sample code here channelItemDuplicateIssue.js · GitHub

Could you also please check the log as well? are there duplicated msgs showing in the log too?

I have checked the code can you share code to call your given function?

Hey @harshit sorry for the late response, you meant this line channelItem("channel ID")

still we are facing message duplication issue

Can you please share the log with us? are the duplicated msg are also showing there? and are you still on version beta.21?

I am on beta.35 facing issue on that version was also facing on .21 as well

you can check this code for reproduce this issue
useEffect(
() =>
observeMessages(channelId, {
onEvent: (
action: MessageAction,
message: Amity.Snapshot<Amity.Message>
) => {
// console.log(‘message>?:::::::’, JSON.stringify(message), action);
// if (action === ‘onCreate’) {

				console.log('message>?:::::::', JSON.stringify(messages) , JSON.stringify(message), action, message.data?.metadata.data.id);
				if (
					messages.filter(item => item.id === message.data?.metadata.data.id)
						?.length > 0
				) {
					return;
				}
				//dispatch({notifyOnNewMessageSent(message.data)});
				// if (message.data?.type == 'image' && message.data?.editedAt) {
				setMessages(prevMessages => [
					message.data?.metadata.data!,
					...prevMessages
				]);
				// } else if (
				//   message.data?.type !== 'image' &&
				//   message.data?.editedAt
				// ) {
				//   setMessages(prevMessages => [
				//     message.data?.metadata.data!,
				//     ...prevMessages,
				//   ]);
				// }
				// }
				if(friendData) {
					dispatch(notifyOnNewMessageSend(message));
				}
			}
		}),
	[channelId, messages]
);

Can you please give us a solution for the duplicate message asap, as we have been facing this issue for a long time? Our client is planning to go live in the next 10 days and we would need a proper solution before going live. Please take this as a priority now. Also if we are not able to fix the issue then please do keep us posted about it as well, so that we can look for other solutions.

any updates on it? we are waiting for your response

Hello @harshit sorry for the late response, from what we have conducted the investigation, this issue doesn’t occur in the newer version(above .35) anymore. However, we are currently working on a known issue that might be related to the msg creation. We will keep you informed on the result.

ok we are waiting for your reply as this bug impact user experience

Can you provide an estimated date on when we can expect the issue resolved? As mentioned our client is planning to go live by 2nd week of feb, so we would need it solved before that in .35 version.

Please try this code below, this should solve the message duplication issue

let tempMid: string = "";
useEffect(() => {
    observeMessages(channelId, {
      onEvent: (action, message) => {
        if (action == "onCreate") {
          if (message.data?.uniqueId === message.data?.messageId) {
            return;
          }

          if (tempMid != message.data?.messageId) {
            tempMid = message.data?.messageId;

            if (messages.find(item => item.messageId == tempMid)) {
              return;
            }

            setMessages((prevMessage) => [message.data, ...prevMessage]);
          }
          
          if(friendData) {
			dispatch(notifyOnNewMessageSend(message));
		  }
        }
      },
    });
  }, [channelId]);