"use strict"; var Chatroom = React.createClass({ getDefaultProps: function () { return { history: [] }; }, componentDidMount: function () { var self = this; var TIMER_INTERVAL = 5000; // Every minute socket.on("message:new", function (data) { var history = self.props.history; history.push(data); self.setProps({ history: history }); self.scrollToBottom(); }); // Message History Retrieved socket.on("message:refresh", function (data) { self.setProps({ history: data.chatHistory }); self.scrollToBottom(); }); socket.emit("message:refresh", {}); self.timer = setInterval(function () { self.forceUpdate(); }, TIMER_INTERVAL); }, componentDidUnmount: function () { clearInterval(this.timer); }, sendMessage: function (message) { socket.emit("newMessage", {message: message}); }, scrollToBottom: function () { var node = React.findDOMNode(this.refs.messageContainer); node.scrollTop = node.scrollHeight; }, render: function () { var messages = this.props.history.map(function (message) { return ( ); }); return (
Gather Chat
    {messages}
); } }); var updateMessageCallbacks = []; var timer = setInterval(function () { updateMessageCallbacks.forEach(function (callback) { callback(); }); }, 60000); var ChatMessage = React.createClass({ componentDidMount: function () { var self = this; updateMessageCallbacks.push(function () { self.forceUpdate(); }); }, render: function () { return (
  • User Avatar
    {this.props.message.author.username} {$.timeago(this.props.message.createdAt)}

    {this.props.message.content}

  • ); } }); var MessageBar = React.createClass({ sendMessage: function (content) { socket.emit("message:new", { content: content }); }, handleSubmit: function (e) { e.preventDefault(); var content = React.findDOMNode(this.refs.content).value.trim(); if (!content) return; React.findDOMNode(this.refs.content).value = ''; this.sendMessage(content); return; }, render: function () { return (
    ); } });