Here, I will be demonstrating an application to chat privately using SignalR. Before that, we need to know first what SignalR is! SignalR is an open and free library that can integrate real-time functionality into your web applications. There are many areas where SignalR can come in handy to make your application more convenient, integrated, and responsive to the end user. Real-time means having your Server respond as quickly as possible to the client as and when a request is made.
For instance, we have a requirement to show the user uploading a file and the percentage of that file uploaded on the Server. Or I had encountered a scenario where we had to show the end user who would upload a CSV file with an ‘n’ number of rows and process each row for some validations.
End user would like to know what is going on in the back-end. So, could we show him how many rows have been processed and how many are left out? Similar to a progress window. Here comes the SignalR magic! Most of us think SignalR would be useful in making chat applications, but NO! It has much more than chat! The makers of SignalR would not have a thought in mind to make a chat application out of it! Much of a story now! Let’s get into a bit of theory!
We will look into a simple image below, and from that image, we will try and gain some knowledge on the flow:
Nowadays, every application requires a load of server response in real time to sustain in the market, as user expectations have risen higher. Remote procedure calls (RPC) is the concept in the SignalR internally. SignalR provides an API that helps make the RPC between the Server and the client. From the client side, server-side functions are called using JavaScript once the connection to the Server is set. The SignalR API also helps create connections and manage them when required. In simple terms, SignalR provides
That somehow is called “Server-Push”. SignalR starts with HTTP and then to a WebSocket if the connection is available. According to Wiki, “WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.” An advantage of using WebSocket is that both client and server applications can use it. WebSocket is considered the most efficient and consistent communication medium as it can manage server memory properly, is a full duplex communication, and has low latency. These are the considerations made with SignalR that make it more efficient. The SignalR decides the transport based on the browser, i.e., if the browsers support the kind of transport required by the SignalR. We will discuss the kinds of transport next:
Comet, usually,is a web application model in which a long-held HTTP request allows the Server to post data to a client (browser).
We will create a chat application to explain the flow of SignalR. We install the SignalR and create a hub to which the client will interact, calling the server methods, and in return, the server responds and interacts with the client. You can directly add a new project in VS for the SignalR or create an MVC project and install the SignalR package/libraries from the Nuget.
PM > Install-Package Microsoft.AspNet.SignalR
This is to download all the dependencies required for the SignalR.
After the successful installation, the above dlls or packages are installed into your project. There will be a class file which needs to be added to the root of your project, which would look like:
This is an OWIN-based application. Every OWIN application will have a startup.cs class, where the components for the application pipeline are added. The OWIN attribute specifies the property type, the project’s start-up, and the configuration method and sets up the SignalR mapping for the App. Another two script files will be added as we install the packages for SignalR.
These script files are mandatory to be loaded onto the .cshtml page to activate the SignalR. Let’s look into the code immediately: We need to add a new hub class inside a Hub folder. Let’s name that LetsChatHub.cs, which would look like:
The above send method accepts the parameters, name (which you would be giving once you navigate onto the url), the message (which the user would be sending from the UI). The other parameter is connId, which would help us have chat private and not send to every user who navigates to the site. If you would like to have every user receive and send the messages to those who navigate to the URL ,To allow every user the access, the code you change as below:
The send method is requested from the client with the parameters after the connection is set on the client side, and once the Server receives the request, it processes and sends back the response to the client using the appendNewMessage. This appendNewMessage method is added on the client side to receive the response and display on the UI to the client. You need to add a controller, lets suppose: “LetsChat” with an action “LetsChat”, add a view to that with the below code. The client-side code would look like below:
We have a normal UI in place to add your message and send button to call the server methods. Let’s understand the code above part by part.
Here, we set the connection to the Hub class. As you can notice, letsChatHub is the same hub class file name we added to set up the Server. The convention as follows, the intial of the methods or the class name starts with lowercase. From here, we use chat to access the Send method.
chat. Server.send,is self explanatory, it sets the chat connection to call the Server Send method once the connection is set and started.
This is called when the Server receives the request and calls back the method on the client side.
The sample provided for download will be a few instructions to follow:
If you want to send a message to all and make that common, use the Clients.All code snippet to send all. Another exciting thing, which was figured out, is the use of @section scripts{}, that lets the Signal R scripts render and active on your page, and also, using the @section scripts provides your code a good outlook.
Ooops!! Nice question, right? Ideally, It is not advised; rather, I would not recommend sending or sharing files using Signal R. There is always a better way to accomplish that. Using API, you can have an upload area and use SignalR to show the progress, and once the upload is complete, update the user regarding the completion and generate a download link on the UI for the users to download and view the file. This is only sometimes the best idea, but it is just another idea.
This is just a simple Chat application you can use to chat with your friends if you host on Azure or any other domain. But again, SignalR is not just more than much. There are a lot of other practical uses of SignalR. I will be sharing more of SignalR’s utility in future articles.
A journey has just begun to create a culture, not an organization.
We at INVINCIX are determined to digitize 500+ start-ups in this era of digitization during the course of the upcoming two fiscal years, FY 2022–2023 and FY 2024–2025.
Dealing directly with the definition and goal of the business.
Efficiency is something that requires constant study.
Deeper comprehension of one another's working methods that will pay off in the long run.
Our goal is to establish a GenInvinci organization with the capacity to "Engage," "Innovate," "Invent," and "Excel"
A sense of satisfaction when you touch the heart with emotion.
We will keep our feet grounded, to ensure, your head is in the cloud.
Honoring the exceptional INVINCIANS
A minute to reflect on the success
It means a lot to us.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Join our Growing Team And Proud To Ba An INVINCIANS.
Let's connect and find the solutions you need. Our team is ready and waiting to assist you with any questions or concerns you may have.
The framework for digital transformation makes excellence in "Innovation," "Agility," and "Responsiveness" possible
The software industry began with the automation of manual tasks. Over time, the automation changed and became known as "Digitalization services"
Experience AD4P : Harness the power of IT to guarantee that your application is prepared for the future
Unique thoughts are valuable and creative thinking are hard to come by. A brilliant idea is always the source of a product
Our consulting will hold your hand and be part of you, through out transformation journey.
Clear emphasis on the experience not on the interface make us a class apart .
As enterprises look to software as a source of competitive advantage, they demand quality-at speed and scale.
Taking relevant info out of a data sea
A keen eye for the bottom line
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Using barcodes to manage inventory quickly
Routing that is easy on the wallet while maintaining client satisfaction
Automating processes to increase efficiency in the service sector
Boost productivity and enable success with the vendor management system
Simplifying the verification and control of your inventory
Connecting world via blood
Implement the “Interpreted” “Inception”
We don’t inspect quality, we inject quality
UX is the key not the interface…
Transforming Database Innovation and Performance / Empowering Data-Driven Excellence
Digital transformation is a business discipline
“Swift and Simple” office automation
Simplify e-Invoicing with our secured SaaS-based APIs
Make your service station digital
Your digital event partner is here
Extracting meaningful data from ocean of data
connecting hospitality industry smoothly
A mini ERP for all your need
Connecting world via blood
Browse our workshop calendar for upcoming events and opportunities to expand your knowledge and skills.
Explore our diverse range of courses designed to cater to various interests, offering opportunities for personal and professional growth
Head Office
Sharadha Bhawan, Rajendra Nagar, Cuttack, Odisha - 753010, India
Branch Office
Plot No.353, 1st Floor, Infocity Ave, Sishu Vihar, Patia, Bhubaneswar, Odisha - 751024, India
Branch Office
60 eu tong sen street #02 03 furama city center Singapore 059804
Branch Office
Amba House, 15 College Road, Harrow, England, HA11BA
Branch Office
02nd Floor, Saigon Paragon Building, No.3 , Nguyen Luong Bang Street, Tan Phu Ward, District 07, Ho Chi Minh City, Vietnam