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, _} -> ...

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.