Build

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

Markus Kohler on Mar 5, 2024
How to Build a Real-time Voting App with .NET and C#

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. It's a web application that brings in the functionality of real-time voting system. Given the rapid technological advancements, it's essential to keep pace and for that, I would like to mention that throughout this tutorial, we are using C# 9, ASP.NET 5.0, and PubNub C# SDK version 4.4.0.

In this tutorial, we’re showcasing how to create a web-based real-time voting application using the C# PubNub API, and ASP.NET MCV4, a part of Microsoft's ASP.NET Core Web API. This will enable users to vote, and the results will be automatically tallied and displayed in real-time, thanks to the PubNub C# SDK. We'll specifically leverage the `publish()`, `Subscribe()` and `DetailedHistory` methods to power our real-time voting app. You can find the source code on GitHub.

In any real-time application, we envisage the question and the choice of answers/responses to originate from a backend like an SQL server database using entity framework. To narrow down our focus in this tutorial, I’ll be hard coding the questions and answer options in the `GetActivePollQuestion()` method of the `SampleData` class, rendering these as xml strings. In a real-world scenario, you would want to return dynamic data as 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

Live Tour

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.