Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content