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

Writing our server channel code for live voting

The last piece to the puzzle for all of this work is that we need to tell Phoenix how to deal with the "vote" message that is getting pushed from the client. Remember that our preceding implementation used the "push" message with an option_id parameter; we'll use those rules to implement the appropriate handle_in function. Back in lib/vocial_web/channels/poll_channel.ex, add the following:

  def handle_in("vote", %{"option_id" => option_id}, socket) do    with {:ok, option} <- Vocial.Votes.vote_on_option(option_id) do      broadcast socket, "new_vote", %{"option_id" => option.id, "votes" => option.votes}      {:reply, {:ok, %{"option_id" => option.id, "votes" => option.votes}}, socket}    else {:error, _} -> ...
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