diff --git a/lib/chat/controller.js b/lib/chat/controller.js index c1817a7..febe926 100644 --- a/lib/chat/controller.js +++ b/lib/chat/controller.js @@ -4,7 +4,7 @@ * Chatroom Controller * * Server API - * message:new - New message needs to be displayed + * message:append - New message to be added to history * message:refresh - Reload all messages * * Client API @@ -20,7 +20,7 @@ var Message = mongoose.model("Message"); module.exports = namespace => { var broadcastUpdate = message => { - namespace.emit("message:new", message); + namespace.emit("message:append", message); }; var refreshMessages = socket => { diff --git a/lib/react/message.jsx b/lib/react/message.jsx index af5f31f..0a287d0 100644 --- a/lib/react/message.jsx +++ b/lib/react/message.jsx @@ -10,11 +10,13 @@ var Chatroom = React.createClass({ componentDidMount() { let self = this; - socket.on("message:new", data => { + socket.on("message:append", data => { let history = self.props.history; history.push(data); self.setProps({ - history: history + history: history.sort((a, b) => { + return a.createdAt - b.createAt; + }) }); self.scrollToBottom(); });