Phoenix Web Development by Brandon Richey
Author:Brandon Richey
Language: eng
Format: epub
Tags: COM060160 - COMPUTERS / Web / Web Programming, COM060080 - COMPUTERS / Web / General, COM051000 - COMPUTERS / Programming / General
Publisher: Packt Publishing
Published: 2018-04-26T05:38:38+00:00
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, _} ->
{:reply, {:error, %{message: "Failed to vote for option!"}}, socket}
end
end
In the preceding block of code, we start with just our handle_in function pattern matching on "vote" messages coming from the client. We're expecting them to provide an option_id parameter (because how else would we know what it is that they're trying to vote on?), and with that option ID we'll try to cast that vote. If we succeed, we'll broadcast out to all listening clients, Hey, we just received a new vote for this particular option ID. The new vote count should be X!
If all went well, we'll broadcast out a message repeating the same information we received, and we'll include a reply to the sender that the server received the message successfully! Otherwise, we'll fail out with an error reply that lets the user know that they failed to vote for the option they clicked the button for!
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9923)
The Mikado Method by Ola Ellnestam Daniel Brolund(9783)
Dependency Injection in .NET by Mark Seemann(9346)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7787)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7563)
Svelte with Test-Driven Development by Daniel Irvine(7220)
Test-Driven Development with PHP 8 by Rainier Sarabia(6949)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6811)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6537)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6422)
Web Development with Django by Ben Shaw Saurabh Badhwar(6277)
React Application Architecture for Production by Alan Alickovic(5997)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5812)
Kotlin in Action by Dmitry Jemerov(5070)
Audition by Ryu Murakami(4588)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4482)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4329)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4322)
Functional Programming in JavaScript by Mantyla Dan(4042)
