diff --git a/bigbluebutton-html5/imports/api/group-chat-msg/server/eventHandlers.js b/bigbluebutton-html5/imports/api/group-chat-msg/server/eventHandlers.js
index 829573fe49..822deedd5c 100644
--- a/bigbluebutton-html5/imports/api/group-chat-msg/server/eventHandlers.js
+++ b/bigbluebutton-html5/imports/api/group-chat-msg/server/eventHandlers.js
@@ -3,10 +3,11 @@ import handleGroupChatsMsgs from './handlers/groupChatsMsgs';
import handleGroupChatMsgBroadcast from './handlers/groupChatMsgBroadcast';
import handleClearPublicGroupChat from './handlers/clearPublicGroupChat';
import handleUserTyping from './handlers/userTyping';
+import handleSyncGroupChatMsg from './handlers/syncGroupsChat';
import { processForHTML5ServerOnly } from '/imports/api/common/server/helpers';
-RedisPubSub.on('GetGroupChatMsgsRespMsg', processForHTML5ServerOnly(handleGroupChatsMsgs));
+RedisPubSub.on('GetGroupChatMsgsRespMsg', processForHTML5ServerOnly(handleSyncGroupChatMsg));
RedisPubSub.on('GroupChatMessageBroadcastEvtMsg', handleGroupChatMsgBroadcast);
RedisPubSub.on('ClearPublicChatHistoryEvtMsg', handleClearPublicGroupChat);
-RedisPubSub.on('SyncGetGroupChatMsgsRespMsg', handleGroupChatsMsgs);
+RedisPubSub.on('SyncGetGroupChatMsgsRespMsg', handleSyncGroupChatMsg);
RedisPubSub.on('UserTypingEvtMsg', handleUserTyping);
diff --git a/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/groupChatMsgBroadcast.js b/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/groupChatMsgBroadcast.js
index 2e0340f5ea..dd116b6ecb 100644
--- a/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/groupChatMsgBroadcast.js
+++ b/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/groupChatMsgBroadcast.js
@@ -1,5 +1,13 @@
import { check } from 'meteor/check';
+import _ from 'lodash';
import addGroupChatMsg from '../modifiers/addGroupChatMsg';
+import addBulkGroupChatMsgs from '../modifiers/addBulkGroupChatMsgs';
+
+const { bufferChatInsertsMs } = Meteor.settings.public.chat;
+
+const msgBuffer = [];
+
+const bulkFn = _.throttle(addBulkGroupChatMsgs, bufferChatInsertsMs);
export default function handleGroupChatMsgBroadcast({ body }, meetingId) {
const { chatId, msg } = body;
@@ -8,5 +16,10 @@ export default function handleGroupChatMsgBroadcast({ body }, meetingId) {
check(chatId, String);
check(msg, Object);
- addGroupChatMsg(meetingId, chatId, msg);
+ if (bufferChatInsertsMs) {
+ msgBuffer.push({ meetingId, chatId, msg });
+ bulkFn(msgBuffer);
+ } else {
+ addGroupChatMsg(meetingId, chatId, msg);
+ }
}
diff --git a/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/syncGroupsChat.js b/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/syncGroupsChat.js
new file mode 100644
index 0000000000..bb0f354370
--- /dev/null
+++ b/bigbluebutton-html5/imports/api/group-chat-msg/server/handlers/syncGroupsChat.js
@@ -0,0 +1,12 @@
+import { Match, check } from 'meteor/check';
+import addBulkGroupChatMsgs from '../modifiers/addBulkGroupChatMsgs';
+
+export default function handleSyncGroupChat({ body }, meetingId) {
+ const { chatId, msgs } = body;
+
+ check(meetingId, String);
+ check(chatId, String);
+ check(msgs, Match.Maybe(Array));
+
+ addBulkGroupChatMsgs(msgs);
+}
diff --git a/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addBulkGroupChatMsgs.js b/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addBulkGroupChatMsgs.js
new file mode 100644
index 0000000000..a51d75be39
--- /dev/null
+++ b/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addBulkGroupChatMsgs.js
@@ -0,0 +1,29 @@
+import { Match, check } from 'meteor/check';
+import { GroupChatMsg } from '/imports/api/group-chat-msg';
+import Logger from '/imports/startup/server/logger';
+import flat from 'flat';
+import { parseMessage } from './addGroupChatMsg';
+
+export default async function addBulkGroupChatMsgs(msgs) {
+ const mappedMsgs = msgs
+ .map(({ chatId, meetingId, msg }) => ({
+ _id: new Mongo.ObjectID()._str,
+ ...msg,
+ meetingId,
+ chatId,
+ message: parseMessage(msg.message),
+ sender: msg.sender.id,
+ }))
+ .map(el => flat(el, { safe: true }));
+
+ try {
+ const { insertedCount } = await GroupChatMsg.rawCollection().insertMany(mappedMsgs);
+ msgs.length = 0;
+
+ if (insertedCount) {
+ Logger.info(`Inserted ${insertedCount} messages`);
+ }
+ } catch (err) {
+ Logger.error(`Error on bulk insert. ${err}`);
+ }
+}
diff --git a/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addGroupChatMsg.js b/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addGroupChatMsg.js
index 9c4eac369e..e0f9a8d55d 100644
--- a/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addGroupChatMsg.js
+++ b/bigbluebutton-html5/imports/api/group-chat-msg/server/modifiers/addGroupChatMsg.js
@@ -4,7 +4,7 @@ import Logger from '/imports/startup/server/logger';
import { GroupChatMsg } from '/imports/api/group-chat-msg';
import { BREAK_LINE } from '/imports/utils/lineEndings';
-const parseMessage = (message) => {
+export function parseMessage(message) {
let parsedMessage = message || '';
// Replace \r and \n to
@@ -15,7 +15,7 @@ const parseMessage = (message) => {
parsedMessage = parsedMessage.split('