Hooking up the new JavaScript code to Phoenix

All of this code doing all of this cool stuff in JavaScript doesn't mean much if we can't actually hook it up for broadcasting and storing messages! The good news is that there really isn't that much code we need to write! First, we'll want to update our chat channel code to be able to handle this new_message event and store the message in the database.

Back in lib/vocial_web/channels/chat_channel.ex, we'll need to add an alias to the Votes context:

alias Vocial.Votes

And then we'll add a new handle_in/3 function:

  def handle_in("new_message", %{"author" => author, "message" => message}, socket) do    with {:ok, _message} <- Votes.create_message(%{author: author, message: message}) do broadcast socket, ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.