Build

How to Build a Real-time Voting App with .NET and C#

2 min read Joe Hanson on Sep 11, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

You can easily build a live real-time voting app in the browser with C# and .NET, enabling users to vote and see results, updated in real time. Have you ever thought of implementing the C# PubNub API ...

real-time voting app

You can easily build a live real-time voting app in the browser with C# and .NET, enabling users to vote and see results, updated in real time.

Have you ever thought of implementing the C# PubNub API with ASP.NET MCV4? Whether you have or not, in this tutorial, we’ll walk you through how to build a web-based real-time voting application. This will enable users to vote, and the results will be automatically tallied and displayed. We’ll be using the PubNub C# SDK for both publish() and DetailedHistory to demo the voting web app.

In a real-time application, we expect the question and the choice of answers/responses come from a database. To keep things simple, I’ll hard code the questions and answer options as a xml string in GetActivePollQuestion() method of SampleData class. In real-time, you can return dynamic data as xml string.

 

Check out our simple real-time voting app below to get a better idea of what you’ll be building. Or feel free to take a look at the real-time voting GitHub Repo:

 

simple real-time voting app

Schemas for a Real-time Voting App

Two schemas PollQuestion.xsd and PollUserAnswer.xsd were created.

PollQuestion.cs and PollUserAnswer.cs class files were generated using xsd.exe tool using the following commands:

Take a look at their schemas below:

Publishing Messages over WebSockets for Real-time Voting

The Publish<string>() method is being used to save the poll answers. The following code saves the answers.

Once the poll answers are submitted to PubNub, the success status of message publish will be displayed as view to the web user as below:

Subscribing to Real-time Voting User Submissions over WebSockets

The Subscribe<string>() method is being used to receive poll answers in real time via an automatically negotiated protocol like WebSockets, HTTP Streaming, HTTP Long-polling, TCP Streaming and more with automatic recovery of dropped messages.  We’ve made it easy for you to get started by providing this easy-to-use SDK.

Loading History of Messages over WebSockets for Real-time Voting Dashboard

The DetailedHistory<string> method is being used to pull the poll results. The following code retrieves the poll results.

And that’s it! We went through both the Publish and DetailedHistory calls for a real-time voting app.

Get Started
Sign up for free and use PubNub to power real-time voting

More from PubNub

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff