{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/join-room-and-receive-messages","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Join Room and Receive Messages\",\n  \"order\": 33,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"react\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Join Room and Receive Messages\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Connect to chatroom\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Receive messages\")), mdx(\"p\", null, \"Now that we have a list of all active presences (we will eventually expand this to include all users, not just currently active presences), we need to give our admin the ability to select one and respond. The UI for this will be clicking on a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"uuid\"), \" on the left that represents a user.\"), mdx(\"p\", null, \"This will subscribe the admin to the topic and allow her to respond to the user. It will also grab the most recent 10 messages from the chatroom and use that to populate our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"messages\"), \".\"), mdx(\"h2\", null, \"Create ChatRoom component\"), mdx(\"p\", null, \"Our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component is already starting to get complicated, so we're going to create a new component called \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" to handle the actual rendering of messages and we'll leave the socket logic in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" for now.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir app/components/ChatRoom\\n$ touch app/components/ChatRoom/{index.js,style.css,README.md,spec.js}\\n\")), mdx(\"p\", null, \"Our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component is going to receive a list of messages as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"props\"), \", which it will then render to the page. Our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"renderMessages\"), \" function maps over each of these messages and returns a jsx component for each.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport style from \\\"./style.css\\\"\\n\\nexport class ChatRoom extends React.Component {\\n  componentDidUpdate() {\\n    if (this.props.messages.length > 0) {\\n      const lastMessage = this[`chatMessage:${this.props.messages.length - 1}`]\\n      this.chatContainer.scrollTop = lastMessage.offsetTop\\n    }\\n  }\\n\\n  render() {\\n    return (\\n      <div\\n        ref={ref => {\\n          this.chatContainer = ref\\n        }}\\n        className={style.chatWrapper}\\n      >\\n        {this.renderMessages(this.props)}\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(ChatRoom, style)\\n\")), mdx(\"p\", null, \"Then we should add a function to map over each of our messages and render them.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport class ChatRoom extends React.Component {\\n  ...\\n  renderMessages() {\\n    return this.props.messages.map(({ body, id }, i) => {\\n      return (\\n        <div\\n          ref={ref => { this[`chatMessage:${i}`] = ref }}\\n          key={id}>\\n          { body }\\n        </div>\\n      )\\n    })\\n  }\\n  ...\\n}\\n...\\n\")), mdx(\"p\", null, \"Then we should add a wrapper to keep this component visible next to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \". Go ahead and remove the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".chatWrapper\"), \" from \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"../Chat/style.css\"), \" as well.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".chatWrapper {\\n  margin-left: 300px;\\n}\\n\")), mdx(\"p\", null, \"The next step is to connect our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component to our room channel and render our new \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component.\"), mdx(\"h2\", null, \"Connecting to channel\"), mdx(\"p\", null, \"Now we need to connect to our room channel, create a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"changeRoom\"), \" function that we can pass to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" that will allow us to change rooms, and render our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component with the list of messages we get from our socket.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nimport { default as ChatRoom } from \\\"../ChatRoom\\\"\\n\\nexport class Chat extends React.Component {\\n  constructor(props) {\\n    super(props)\\n    this.state = {\\n      presences: {},\\n      messages: [],\\n      currentRoom: \\\"\\\"\\n    }\\n    this.changeChatroom = this.changeChatroom.bind(this)\\n  }\\n\\n  ...\\n\\n  changeChatroom(room) {\\n    this.channel = this.socket.channel(`room:${room}`)\\n    this.setState({\\n      messages: []\\n    })\\n    this.configureRoomChannel(room)\\n  }\\n  ...\\n}\\n\")), mdx(\"p\", null, \"Now we need to configure our room channel to take in this new value.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport class Chat extends React.Component {\\n  ...\\n\\n  configureRoomChannel(room) {\\n    this.channel.join()\\n      .receive(\\\"ok\\\", ({ messages }) => {\\n        console.log(`Succesfully joined the ${room} chat room.`, messages)\\n        this.setState({\\n          messages,\\n          currentRoom: room\\n        })\\n      })\\n      .receive(\\\"error\\\", () => { console.log(`Unable to join the ${room} chat room.`) })\\n\\n    this.channel.on(\\\"message\\\", payload => {\\n      this.setState({\\n        messages: this.state.messages.concat([payload])\\n      })\\n    })\\n  }\\n\\n  ...\\n}\\n...\\n\")), mdx(\"p\", null, \"And finally we need to pass our click handler to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" and pass our messages to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport class Chat extends React.Component {\\n  ...\\n  render() {\\n    return (\\n      <div>\\n        <Sidebar\\n          presences={this.state.presences}\\n          onRoomClick={this.changeChatroom} />\\n        <ChatRoom messages={this.state.messages} />\\n        { this.props.children }\\n      </div>\\n    )\\n  }\\n  ...\\n}\\n...\\n\")), mdx(\"p\", null, \"The last change we need to make is to add an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"onClick\"), \" to each of our presence components in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" list.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nconst renderList = props => {\\n  return Presence.list(props.presences, listBy)\\n    .map(({ id }) => {\\n      return (\\n        <div\\n          onClick={() => { props.onRoomClick(id) }}\\n          key={id}>\\n          { id }\\n        </div>\\n      )\\n    })\\n}\\n...\\n\")), mdx(\"p\", null, \"At this point, our admin can receive messages. You should see an anonymous user's id appear in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" and you should be able to click on that and see incoming messages. You can send messages to this admin from your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat\"), \" component.\"), mdx(\"p\", null, \"The next step is to allow our admin to respond.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Join Room and Receive Messages Connect to chatroom Receive messages Now that we have a list of all active presences (we will eventually…","frontmatter":{"title":"Join Room and Receive Messages","order":33,"active":true,"free":false,"tech":["react"]}}},"pageContext":{"slug":"join-room-and-receive-messages","prevNode":{"frontmatter":{"title":"Connect the Frontend to AdminChannel","active":true,"free":false,"order":32,"tech":["react","phoenix"]},"fields":{"slug":"connect-the-frontend-to-adminchannel"}},"nextNode":{"frontmatter":{"title":"Change Room and Respond to Messages","active":true,"free":false,"order":34,"tech":["react"]},"fields":{"slug":"change-room-and-respond-to-messages"}}}},"staticQueryHashes":["1723017916"]}