diff --git a/static/js/message_list.js b/static/js/message_list.js index 655526d642..2dabd09b05 100644 --- a/static/js/message_list.js +++ b/static/js/message_list.js @@ -55,7 +55,11 @@ exports.MessageList.prototype = { return; } - // Put messages in correct order on either side of the message list + // Put messages in correct order on either side of the + // message list. This code path assumes that messages + // is a (1) sorted, and (2) consecutive block of + // messages that belong in this message list; those + // facts should be ensured by the caller. if (self.empty() || msg.id > self.last().id) { bottom_messages.push(msg); } else if (msg.id < self.first().id) { diff --git a/static/js/server_events.js b/static/js/server_events.js index 9cd980cd92..358d4adb76 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -89,6 +89,10 @@ function get_events_success(events) { }); if (messages.length !== 0) { + // Sort by ID, so that if we get multiple messages back from + // the server out-of-order, we'll still end up with our + // message lists in order. + messages = _.sortBy(messages, 'id'); try { messages = echo.process_from_server(messages); message_events.insert_new_messages(messages);