In this episode, we roll up our sleeves and dig into actually storing chat messages! We start by wiring up our chat input so that when a user submits a message, we gather up the message body and send it to a new function. At first, we just console.log things to make sure our data looks good, then we move on to creating the API endpoint and controller that will actually save the message on the backend.
After setting up the backend route and controller, we validate the incoming message (think: making sure it's not empty and maybe limiting its length), and then persist it to the database—linking it to the current user and chat room. We also touch on best practices for fillable fields in our models. Once everything looks good on the backend, we tweak the frontend store action to post the new message and—when it comes back successfully—push it onto our messages array so it appears instantly in the chat.
Oh, and we cover why new messages might actually go at the beginning of the array, since our message list uses CSS flex-reverse. By the end of the episode, submitting a message will save it in the database and show it up right away in the chat window. Next stop: handling this in real time!