How to Build a Real-time Voting App with .NET and C#
Simple live audience polling via an Internet browser
You can easily create a live, real-time voting app in the browser using C# and .NET, allowing users to vote and see results updated instantly. In this tutorial, we’ll use C# 9, ASP.NET 5.0, and the PubNub C# SDK version 4.4.0 to build a real-time voting system.
Learn how to create a web-based voting app with the C# PubNub API and ASP.NET MVC4, part of the ASP.NET Core Web API. Users can vote, and results will be tallied and displayed in real-time using PubNub’s publish()
, subscribe()
, and DetailedHistory
methods. The source code is available on GitHub.
In a real-time application, questions and answers typically come from a backend database like SQL using Entity Framework. For this tutorial, I'll hardcode the questions and answers in the GetActivePollQuestion()
method of the SampleData
class, rendering them as XML strings. In a real-world scenario, dynamic data would be returned as an XML string.
Schemas for a Real-time Voting App
In this context, schemas pave the way for organized and structured data storage, enabling efficient data handling operations. To accomplish our objectives, two schemas, PollQuestion.xsd and PollUserAnswer.xsd were created. These provide a blueprint for our data, outlining the structure and type of information stored.
The corresponding class files, PollQuestion.cs and PollUserAnswer.cs, were generated using the following commands in the Visual Studio terminal via the xsd.exe tool:
Take a look at the formats of these schemas:
Publishing Messages over WebSockets for Real-time Voting
The `Publish()` method plays a crucial role in our real-time voting application as it is used to send the poll answers to a specified PubNub channel. This message is then received by all instances subscribed to that channel in real time, facilitating an interactive voting experience. You can easily configure this operation with the following C# code segment.
Subscribing to Real-time Voting User Submissions over WebSockets
On the other side of the equation, the `Subscribe()` method is used for receiving poll answers in real time. This enables our application to provide real-time updates. The PubNub SDK can auto-recover any message/link that is dropped using long polling among other protocols. To get the functionality of this in your C# code you can add the following code snippet.
Loading History of Messages over WebSockets for Real-time Voting Dashboard
The `DetailedHistory` method fetches historical messages from a channel. This method fetches these messages asynchronously, allowing us to see the history of voting data from a specified PubNub channel. Here's the code segment demonstrating this:
Wrapping Up
By now, you should have a good understanding of how to build a real-time voting application using C#, .NET, and the PubNub C# SDK. If you have followed the tutorial thoroughly, you would have noticed how simple it is to integrate real-time features into your applications using PubNub.
In the next blog post, we'll take this a notch further by incorporating additional features such as Angular on the front-end, add user authentication, configure data encryption, and much more. Stay tuned to our playlist of web development tutorials!
Getting Started
Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices. It's particularly useful for building a real-time voting application like the one we'll be discussing in this tutorial.
The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. And you can depend on us for robust, high-performance solutions.
Experience PubNub
Get Setup
To start creating your own real-time applications, sign up for a PubNub account.
Get Started
The PubNub docs are there to help you, no matter what you're building. We offer extensive guides for different use cases and SDKs, including C# which we used in this tutorial.